将元组传递给ObjectDataProvider参数

时间:2019-02-25 22:04:19

标签: c# wpf objectdataprovider

我的transactionProductspublic 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个Label中,我需要Label 3的值作为ObjectDataProvider的结果,但是getPrice方法需要将Item1和Item2放入其中。我尝试了<system:Int16>Item1</system:Int16>,但是说输入字符串是格式不正确。也可以传入整个元组。我该如何实现?

1 个答案:

答案 0 :(得分:0)

您可以使用 let myJsonString = "{ "headers": { ... }, "body": { "RequestInfo": { ..., "Identificator": null } } }" const parsedJsonValue = JSON.parse(myJsonString); const {body} = parsedJsonValue; if(body && body.RequestInfo) { const {Identificator} = body.RequestInfo; if(Identificator === null || Identificator === undefined) { // Identificator is null } } 和一个多值转换器,该转换器接受一组值并返回一个值:

MultiBinding

XAML:

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();
    }
}