【发布时间】: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