我的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>
,但这说明输入字符串不正确格式。通过整个元组也可以接受。我该如何实现?
您可以使用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>