列表项的多重绑定

时间:2015-10-26 23:38:09

标签: c# wpf xaml binding multibinding

<MultiBinding Converter="{StaticResource DifferenceConverter}">
        <Binding Path="[1].Value" />
        <Binding Path="[0].Value" />
</MultiBinding>

我有一个列表List<List<KeyValuePair<string, int>>>,它绑定到WPF Toolkit图表的DataContext

列表中的每个项目 - 新ColumnSerie和绑定我尝试仅应用于上一个系列

我需要什么 - 正确使用MultiBinding(不知道如何写出列表中第一和第二项值的正确路径)。

还试过像[1]/Value一样的smth - 不成功。

更新(详细说明):

正如我之前写的,我有几个columnSeries的图表(实际上只有2个)。 我将List<List<KeyValuePair<string, int>>>(size = 2)绑定到图表DataContext,并将XAML init ColumnSeries绑定到ItemsSource="{Binding [1]}"

对于Second ColumnSeries,我创建Style:

<Style x:Key="ColumnDataPointStyle1" TargetType="{x:Type chartingToolkit:ColumnDataPoint}">
        <Setter Property="Background" Value="#61596f" />
        <Setter Property="BorderBrush" Value="#61596f" />
        <Setter Property="BorderThickness" Value="100" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="Margin" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type chartingToolkit:ColumnDataPoint}">
                    <Grid>
                        <Rectangle Fill="{TemplateBinding Background}" />
                        <TextBlock  Foreground="White" FontSize="8" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center"> 
                            <TextBlock.Text>
                                <MultiBinding Converter="{StaticResource DifferenceConverter}">
                                    <Binding ElementName="FirstScan" Path="ItemsSource[0].Value"/>
                                    <Binding Path="Value" />
                                </MultiBinding>
                            </TextBlock.Text>   
                        </TextBlock>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

转换器:

public class DifferenceConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var diff = (int) values[1] - (int) values[0];
        if (diff > 0)
        {
            return "<";
        }
        if (diff < 0)
        {
            return ">";
        }
        return "none";
    }

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

此转换器应显示第一和第二系列中列值的差异。如果第一个值大于第二个 - &#34;&gt;&#34;否则&#34;&lt;&#;;如果等于 - &#34;无&#34;等等 问题 - 需要绑定第一个ColumnSeries的值。

1 个答案:

答案 0 :(得分:0)

的Xaml:

        <TextBlock>
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource MultiBindingConverter}">
                    <Binding Path="[0][0].Value" />
                    <Binding Path="[0][1].Value" />
                    <Binding Path="[0][2].Value" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>

C#转换器:

public class MultiBindingConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        int value1 = (int)values[0];
        int value2 = (int)values[1];
        int value3 = (int)values[2];



        return null;
    }

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

====================================== 更新

的Xaml:

<Window x:Class="WpfToolkitChart.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfToolkitChart"
    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"

    Title="MainWindow" Height="1031" Width="855" 
    >

<Window.Resources>

    <local:DifferenceConverter x:Key="DifferenceConverter" />

    <Style x:Key="ColumnDataPointStyle1" TargetType="{x:Type chartingToolkit:ColumnDataPoint}">
        <Setter Property="Background" Value="#61596f" />
        <Setter Property="BorderBrush" Value="#61596f" />
        <Setter Property="BorderThickness" Value="100" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="Margin" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type chartingToolkit:ColumnDataPoint}">
                    <Grid>
                        <Rectangle Fill="{TemplateBinding Background}" />
                        <TextBlock  Foreground="White" FontSize="8" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Top">
                            <TextBlock.Text>
                                <MultiBinding Converter="{StaticResource DifferenceConverter}">

                                    <!--Get firstScan all item-->
                                    <Binding ElementName="FirstScan" Path="ItemsSource"/>

                                    <!--Get secondScan all item-->
                                    <Binding ElementName="SecondScan" Path="ItemsSource"/>

                                    <!--Get secondScan current KeyValuePair-->
                                    <Binding Path="" />

                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>

<Grid>

    <chartingToolkit:Chart Height="262" 
                            Name="columnChart" 
                            Title="Column Series Demo" 
                            >


        <chartingToolkit:ColumnSeries x:Name="FirstScan"
                                        DependentValuePath="Value" 
                                        IndependentValuePath="Key"
                                        ItemsSource="{Binding [0]}"
                                        />

        <chartingToolkit:ColumnSeries x:Name="SecondScan"
                                        DependentValuePath="Value" 
                                        IndependentValuePath="Key"
                                        ItemsSource="{Binding [1]}"
                                        DataPointStyle="{StaticResource ColumnDataPointStyle1}"
                                        >

        </chartingToolkit:ColumnSeries>

    </chartingToolkit:Chart>

</Grid>

C#:

public partial class MainWindow : Window
{
    public List<List<KeyValuePair<string, int>>> ViewModel { get; private set; }

    public MainWindow()
    {
        InitializeComponent();
        DataContextChanged += MainWindow_DataContextChanged;

        var viewModel = new List<List<KeyValuePair<string, int>>>();

        for (int f = 0; f < 1; f++)
        {
            var item = new List<KeyValuePair<string, int>>();

            item.Add(new KeyValuePair<string, int>($"{f}, Key1", 1));
            item.Add(new KeyValuePair<string, int>($"{f}, Key2", 2));
            item.Add(new KeyValuePair<string, int>($"{f}, Key3", 3));

            viewModel.Add(item);
        }

        for (int f = 1; f < 2; f++)
        {
            var item = new List<KeyValuePair<string, int>>();

            item.Add(new KeyValuePair<string, int>($"{f}, Key2", 2));
            item.Add(new KeyValuePair<string, int>($"{f}, Key1", 1));
            item.Add(new KeyValuePair<string, int>($"{f}, Key3", 3));

            viewModel.Add(item);
        }


        DataContext = viewModel;
    }

    private void MainWindow_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        ViewModel = DataContext as List<List<KeyValuePair<string, int>>>;
    }

    private void ButtonAdd_Click(object sender, RoutedEventArgs e)
    {
        for (int f = 0; f < ViewModel.Count; f++)
        {
            var item = new KeyValuePair<string, int>($"{f}, New Key", 1);

            ViewModel[f].Add(item);
        }

    }

    private void ButtonModify_Click(object sender, RoutedEventArgs e)
    {
        for (int f = 0; f < ViewModel.Count; f++)
        {
            for (int j = 0; j < ViewModel[f].Count; j++)
            {
                ViewModel[f][j] = new KeyValuePair<string, int>($"{f}, Modify key", 1);
            }
        }

    }
}

public class DifferenceConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        // Get first and second column ItemsSource
        var firstColumnItemsSource = (List<KeyValuePair<string, int>>)values[0];
        var secondColumnItemsSource = (List<KeyValuePair<string, int>>)values[1];

        // Get second  column current KeyValuePair
        var secondKeyValuePair = (KeyValuePair<string, int>)values[2];

        // Get second keyvaluePair index of second column ItemsSource
        int index = secondColumnItemsSource.IndexOf(secondKeyValuePair);

        // Get two column value
        int firstValue = firstColumnItemsSource[index].Value;
        int secondValue = secondKeyValuePair.Value;

        // Get difference value
        var diff = secondValue - firstValue;

        if (diff < 0)
        {
            return "<";
        }
        else if (diff > 0)
        {
            return ">";
        }
        else
        {
            return "none";
        }
    }

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

Output