0

My transactionProductsis a public ObservableCollection<Tuple<int, Product, bool>> transactionProducts { get; set; } = new ObservableCollection<Tuple<int, Product, bool>> { }; And My window xaml 如下所示:

<Window x:Class="NX_Kassa.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:NX_Kassa"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="NX Kassa" Height="720" Width="1280" WindowStartupLocation="CenterScreen">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="80*" />
            <ColumnDefinition Width="20*" />
        </Grid.ColumnDefinitions>
        <ListBox ItemsSource="{Binding transactionProducts }" Name="TransactionDisplay" HorizontalContentAlignment="Stretch">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Item3}" Value="True">
                            <Setter Property="Background" Value="DeepSkyBlue" />
                            <Setter Property="Foreground" Value="White" />
                        </DataTrigger>
                    </Style.Triggers>
                    <Style.Resources>
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="White"/>
                        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="White" />
                    </Style.Resources>
                    <Setter Property="BorderThickness" Value="0"></Setter>
                    <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_MouseDoubleClick" />
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.Template>
                <ControlTemplate>
                    <DockPanel LastChildFill="True">
                        <Grid DockPanel.Dock="Top" Height="28" Background="LightGray">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="85*"></ColumnDefinition>
                                <ColumnDefinition Width="5*"></ColumnDefinition>
                                <ColumnDefinition Width="10*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Label Grid.Column="0" Padding="10,0" VerticalContentAlignment="Center">Nimetus</Label>
                            <Label Grid.Column="1" Padding="5,0" VerticalContentAlignment="Center">Kogus</Label>
                            <Label Grid.Column="2" Padding="5,0" VerticalContentAlignment="Center">Hind</Label>
                        </Grid>
                        <ItemsPresenter></ItemsPresenter>
                    </DockPanel>
                </ControlTemplate>
            </ListBox.Template>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="85*"></ColumnDefinition>
                            <ColumnDefinition Width="5*"></ColumnDefinition>
                            <ColumnDefinition Width="10*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Label Foreground="{Binding Path=Foreground,
                                            RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Padding="5,0" VerticalContentAlignment="Center" Height="28" Content="{Binding Item2.DisplayName}" Grid.Column="0"></Label>
                        <Label Foreground="{Binding Path=Foreground,
                                            RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Padding="10,0" VerticalContentAlignment="Center" Height="28" Content="{Binding Item1}" Grid.Column="1"></Label>
                        <Label Foreground="{Binding Path=Foreground,
                                            RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Padding="10,0" VerticalContentAlignment="Center" Height="28" Content="{Binding Parent.getPrice}" Grid.Column="2"></Label>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <local:ProductButtonGrid Grid.Column="1"></local:ProductButtonGrid>
    </Grid>

    <Window.Resources>
        <ObjectDataProvider ObjectType="{x:Type local:Transaction}"
        MethodName="ConvertTemp" x:Key="getPrice">
            <ObjectDataProvider.MethodParameters>

            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
</Window>

在最后 3 个标签中,我需要将标签 3 的值作为 ObjectDataProvider 的结果,但是 getPrice 方法需要将 Item1 和 Item2 放入其中……我试过<system:Int16>Item1</system:Int16>了,但这表示输入字符串的格式不正确。传递整个元组也是可以接受的。我怎么能做到这一点?

4

1 回答 1

0

您可以使用一个MultiBinding和一个接受值数组并返回一个值的多值转换器:

public class MultiValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return Transaction.ConvertTemp((int)values[0], values[1] as Product);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

XAML:

<Label>
    <Label.Content>
        <MultiBinding>
            <MultiBinding.Converter>
                <local:MultiValueConverter />
            </MultiBinding.Converter>
            <Binding Path="Item1" />
            <Binding Path="Item2" />
        </MultiBinding>
    </Label.Content>
</Label>
于 2019-02-26T13:53:41.017 回答