Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 将多个源绑定到ListView_Wpf_Xaml_Binding_Datatemplate - Fatal编程技术网

Wpf 将多个源绑定到ListView

Wpf 将多个源绑定到ListView,wpf,xaml,binding,datatemplate,Wpf,Xaml,Binding,Datatemplate,我有一个带有dataTemplate的ListView,我需要将它绑定到3个具有相同索引的不同源。我想我必须在XAML中完全做到这一点,因为源代码(chart)只存在于XAML中。我正在使用MVVM模式。“ 我已经写下了它应该如何工作,索引I是常用键 <ListView ItemsSource="{Binding ???}"> <ListView.ItemTemplate> <DataTemplate> <StackP

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

Mh,让我们看看。如何将listview绑定到
chart.Series
,这样可以获得正确数量的元素。然后在数据模板中绑定序列的属性。对于行为,可以使用
多重绑定
和转换器提取数据

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


现在,您应该将
图表
和当前序列对象传递到转换器中,在那里您应该能够执行类似
var idx=chart.series.IndexOf(s)的操作
这样您就可以访问行为中的对应点。

您使用的是MVVM。那么,您不应该为这种情况创建一个ViewModel吗?类,包含序列和行为。然后使用此ViewModel作为ListView的绑定源。我有一个ViewModel,它只为图表控件提供数据,但图表会创建颜色和对象只有图表知道当前选择的Y点。并且图表只在视图中知道,所以?