2015-10-26 83 views
0
<MultiBinding Converter="{StaticResource DifferenceConverter}"> 
     <Binding Path="[1].Value" /> 
     <Binding Path="[0].Value" /> 
</MultiBinding> 

我有列表List<List<KeyValuePair<string, int>>>,我將它綁定到WPF Toolkit圖表的DataContextMultiBinding for list items

在列表中的每個項目 - 新ColumnSerie並結合我儘量只適用於過去的意甲

我需要什麼 - 正確使用MultiBinding(不知道如何正確的路徑寫在第一和第二項的值。列表)。

也嘗試像[1]/Value - 不成功。


UPDATE(詳解):

正如我以前寫的,我有很少的ColumnSeries圖(真的只有2)。 我結合List<List<KeyValuePair<string, int>>>(大小= 2)以圖表DataContext和XAML初始化的ColumnSeries像ItemsSource="{Binding [1]}"

對於二的ColumnSeries創建風格:

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

該轉換器應該顯示第一和第二系列的列值之間的差異。如果第一個值大於秒 - 「>」else「<」,if Equal - 「none」等。 問題 - 需要綁定來自第一個ColumnSeries的值。

+0

[0]會給你第一列表>和[0] [0]。將在整個列表中給你第一個KeyValuePair,[0]。[0] .Value應該給你第一個KeyValuePair的值。 – AnjumSKhan

+0

@AnjumSKhan,謝謝,現在我明白了。但還有一個問題(請檢查更新問題)我需要綁定來自另一個控件(columnSerie) – demo

+0

值不知道該工具包,但你可以使用這個http://www.codeproject.com/Articles/196502/WPF- Toolkit-Charting-Controls-Line-Bar-Area-Pie-Co – AnjumSKhan

回答

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

+0

感謝您的回覆!但它不起作用。在轉換器中,我得到'DependencyProperty.UnsetValue'的集合,而不是一些int值 – demo

+0

2Update:我是否正確如果我說SecondScan Converter的每個元素都會接收第一次和第二次掃描的所有項目?性能不好嗎? – demo

+0

@demo,MultiBinding只傳遞一個對象引用,但每次更改都會觸發轉換器掃描所有項目。使用自定義索引字段進行綁定比'IndexOf'功能更好。 – Bobson