Передайте Tuple в параметры ObjectDataProvider - PullRequest
0 голосов
/ 26 февраля 2019

My transactionProducts - это public ObservableCollection<Tuple<int, Product, bool>> transactionProducts { get; set; } = new ObservableCollection<Tuple<int, Product, bool>> { }; И 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>, но это говорит о том, что входная строка не в правильном формате.Передача всего кортежа также допустима.Как мне этого добиться?

1 Ответ

0 голосов
/ 26 февраля 2019

Вы можете использовать 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>
...