【问题标题】:Binding multiple Sources to a ListView将多个源绑定到 ListView
【发布时间】:2011-07-27 11:43:19
【问题描述】:

我有一个带有 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>

【问题讨论】:

  • 您正在使用 MVVM.. 那么您不应该为这种情况创建一个 ViewModel 吗?包含 Series 和 Behaviour 的类。然后将此 ViewModel 用作 ListView 的绑定源。
  • 我有一个 ViewModel,它只为图表控件提供数据,但图表创建颜色,只有图表知道当前选择了哪个 Y 点。而且图表只在视图中是已知的,所以呢?

标签: wpf xaml binding datatemplate


【解决方案1】:

嗯,让我们看看。你如何将你的列表视图绑定到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) 之类的操作,以便您可以访问行为中的相应点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    相关资源
    最近更新 更多