【发布时间】:2011-08-10 17:37:08
【问题描述】:
快速提问...
我有一个ListBox,其ItemsSource 属性绑定到视图模型中的集合属性,如下所示:
<ListBox Name="CollectionsListBox" ItemsSource="{Binding Activity.Timesheets}" />
我在同一个视图中还有两个 Button 对象。问题是...我可以仅使用 XAML 将 CollectionsListBox ItemsSource Binding 从 Activity.Timesheets 更改为 Activity.Attachments 吗?
失败了,从视图模型中使用命令对象?
编辑>>>
我在霍华德的部分回答中使用RadioButtons 而不是Buttons 找到了一个简单的解决方案:
<ListBox Name="CollectionsListBox">
<ListBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=TimesheetsButton,Path=IsChecked}" Value="True">
<Setter Property="ListBox.ItemsSource" Value="{Binding Activity.Timesheets}" />
<Setter Property="ListBox.ItemContainerStyle" Value="{StaticResource TimesheetStyle}" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=AttachmentsButton,Path=IsChecked}" Value="True">
<Setter Property="ListBox.ItemsSource" Value="{Binding Activity.Attachments}" />
<Setter Property="ListBox.ItemContainerStyle" Value="{StaticResource AttachmentStyle}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.Style>
</ListBox>
非常感谢您的帮助。
【问题讨论】:
标签: wpf data-binding listbox itemssource