2011-04-01 33 views
0

我有一个ListView和一个dataTemplate,我需要绑定到3个不同的来源,具有相同的索引。我认为我必须在XAML中完全执行此操作,因为源(chart)仅存在于xaml中。我使用MVVM模式。”
我已经记下了它是如何‘应该’的工作,该指数i是公共密钥。将多个源绑定到ListView

<ListView ItemsSource="{Binding ???}"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
     <StackPanel> 
      <!-- Small rectangle filled with the same color as the corresponding line --> 
      <Rectangle 
       Height="10" 
       Width="10" 
       Fill="{Binding ElementName=chart, Path=Series[i].LineStroke}" /> 
      <!-- The title of the corresponding line --> 
      <TextBlock 
       x:Name="Title" 
       Text="{Binding ElementName=chart, Path=Series[i].DataSeries.Title}" /> 
      <!-- The actual value of the corresponding line on the current position--> 
      <TextBlock 
       x:Name="Value" 
       Text="{Binding ElementName=chart, Path=Behaviour.Behaviours[0].CurrentPoints[i].Y}" /> 
     </StackPanel> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 
+0

您正在使用MVVM ..所以你不应该为这种情况创建一个** ViewModel **?包含Series和Behavior的类。然后使用此ViewModel作为ListView的绑定源。 – Damb 2011-04-01 07:42:19

+0

我有一个ViewModel仅为图表控件提供数据,但图表创建颜色,只有图表知道当前选择了哪个Y点。图表只在视图中是已知的,所以? – chriszero 2011-04-01 07:50:55

回答

2

MH,让我们看看。你怎么样绑定你的ListView到chart.Series这样你得到的元素的权数。一个那么在您的数据模板绑定系列的性能。对于行为,你可以使用MultiBinding和转换器来提取数据

<ListView ItemsSource="{Binding Path=Series, ElementName=chart}"> 
<ListView.ItemTemplate> 
    <DataTemplate> 
    <StackPanel> 
     <!-- Small rectangle filled with the same color as the corresponding line --> 
     <Rectangle 
      Height="10" 
      Width="10" 
      Fill="{Binding Path=LineStroke}" /> 
     <!-- The title of the corresponding line --> 
     <TextBlock 
      x:Name="Title" 
      Text="{Binding Path=DataSeries.Title}" /> 
     <!-- The actual value of the corresponding line on the current position--> 
     <TextBlock x:Name="Value"> 
      <TextBlock.Text> 
       <MultiBinding Converter="{StaticResource ChartSeriesBehaviourConverter}"> 
       <Binding ElementName=chart/> 
       <Binding/> 
       <MultiBinding> 
     </TextBlock> 
    </StackPanel> 
    </DataTemplate> 
</ListView.ItemTemplate> 
</ListView> 

现在你应该得到的chart和当前系列对象传递int o你的转换器,你应该能够做到像var idx = chart.Series.IndexOf(s)这样的东西,这样你就可以访问行为中的相应点。