将多个源绑定到ListView

时间:2011-04-01 07:23:06

标签: wpf xaml binding datatemplate

我有一个带有dataTemplate的ListView,我需要将它绑定到具有相同索引的3个不同源。我想我必须在XAML中完全执行此操作,因为源(chart)仅存在于xaml中。我正在使用MVVM模式。“ 我已经写下它应该如何工作,索引i是共同的关键。

<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>

1 个答案:

答案 0 :(得分:2)

嗯,让我们看看。如何将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和当前系列对象传递到您的转换器中,您应该能够执行var idx = chart.Series.IndexOf(s)之类的操作,以便您可以访问行为中的相应点。

相关问题