首先有一个转换器,它将比较两个对象的引用说ObjectEqualsConverter
public class ObjectEqualsConverter : IMultiValueConverter
{
#region IMultiValueConverter
public object Convert(object[] values, Type targetType, object parameter,
CultureInfo culture)
{
return values[0] == values[1];
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
在XAML
文件中,使用转换器检查转换器是否current item is same as highlighted item in ViewModel
返回真设置color
控制使用trigger
-
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<myUserControl x:Name="myControl" />
<DataTemplate.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource ObjectEqualsConverter}">
<Binding/>
<Binding Path="DataContext.HighlightedItem" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter TargetName="myControl" Property="Background" Value="Red"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
确保resource
在 xaml 文件中添加转换器。