[英]WPF Highlight Item
I have a ViewModel that provides a collection of Items. 我有一个ViewModel提供项目的集合。 There is also a ActiveItem propery.
还有一个ActiveItem属性。 The Items collection may or may not contain ActiveItem.
Items集合可能包含或可能不包含ActiveItem。
What I want to do (in XAML) is display the items as a list and highlight any of the items that are equal to Active Item. 我想做的(在XAML中)是将项目显示为列表,并突出显示与“活动项目”相同的任何项目。
I have tried the following with no success: 我尝试了以下方法,但均未成功:
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border x:Name="outerBorder" Background="Green">
<TextBlock Text="{Binding ItemId}" />
</Border>
<DataTemplate.Triggers>
<DataTrigger
Binding="{Binding
Path=DataContext.Item.ItemId,
RelativeSource={RelativeSource TemplatedParent},
Mode=Default}"
Value="{Binding
Path=DataContext.ActiveItem.ItemId,
RelativeSource={RelativeSource AncestorType=Window},
Mode=Default}"
>
<Setter TargetName="outerBorder"
Property="Background" Value="Orange" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
As you can see I attempted to use a DataTrigger to match the current item with the ActiveItem but it doesn't work. 如您所见,我尝试使用DataTrigger将当前项与ActiveItem匹配,但是它不起作用。 I think this is something to do with my trying to use a Binding in DataTrigger.Value - something I haven't seen any other examples of.
我认为这与我尝试在DataTrigger.Value中使用Binding有关-我没有见过其他示例。
Any ideas how I might make this work? 有什么想法可以使我工作吗?
Thanks, 谢谢,
Daniel 丹尼尔
Since you're using MVVM, why not just have the view model expose a property telling the view whether it's active or not? 由于您使用的是MVVM,为什么不只让视图模型公开一个告诉视图是否处于活动状态的属性? That'll get that logic out of you view and into your VM.
这样一来,逻辑就可以从您的视图进入VM。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.