C# 基于ViewModel上的属性更改ListBoxItem颜色
我有这样一个列表框:C# 基于ViewModel上的属性更改ListBoxItem颜色,c#,wpf,datatrigger,C#,Wpf,Datatrigger,我有这样一个列表框: <ListBox ItemsSource="{Binding Users}" SelectedItem="{Binding CurrentSelectedUser}" DisplayMemberPath="Username"/> 这不管用。有没有办法做到这一点?我知道Value不是依赖属性。但是我想做这样的事情 使用多值转换器,传递这两个值并比较,然后返回值。它没有编译,因为值不是依赖性属性,表示不能在非依赖性属性中使用绑定 您可
<ListBox ItemsSource="{Binding Users}" SelectedItem="{Binding CurrentSelectedUser}"
DisplayMemberPath="Username"/>
这不管用。有没有办法做到这一点?我知道
Value
不是依赖属性。但是我想做这样的事情 使用多值转换器,传递这两个值并比较,然后返回值。它没有编译,因为值不是依赖性属性,表示不能在非依赖性属性中使用绑定
您可以使用IMultiValueConverter根据收到的参数返回颜色,下面是一个示例
转换器:
public class Converter : IMultiValueConverter
{
public Converter()
{
}
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var currentPersonName = values[0].ToString();
var listItemPersonName = values[1].ToString();
return currentPersonName == listItemPersonName ? Brushes.Red : Brushes.Black;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Window.Resources>
<local:Converter x:Key="converter"/>
<Style x:Key="style" TargetType="ListBoxItem">
<Setter Property="Foreground">
<Setter.Value>
<MultiBinding Converter="{StaticResource converter}">
<MultiBinding.Bindings>
<Binding Path="DataContext.CurrentPerson.UserName"
RelativeSource="{RelativeSource AncestorType={x:Type Window}}"/>
<Binding Path="UserName"/>
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<ListBox ItemsSource="{Binding Persons}"
DisplayMemberPath="{Binding UserName}"
ItemContainerStyle="{StaticResource style}"
SelectedItem="{Binding SelectedPerson}">
</ListBox>
在这里,您将通过参数接收这两个名称,因此您可以比较并返回所需的颜色
通过多重绑定传递这两个值,这里是XAML
XAML:
public class Converter : IMultiValueConverter
{
public Converter()
{
}
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var currentPersonName = values[0].ToString();
var listItemPersonName = values[1].ToString();
return currentPersonName == listItemPersonName ? Brushes.Red : Brushes.Black;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Window.Resources>
<local:Converter x:Key="converter"/>
<Style x:Key="style" TargetType="ListBoxItem">
<Setter Property="Foreground">
<Setter.Value>
<MultiBinding Converter="{StaticResource converter}">
<MultiBinding.Bindings>
<Binding Path="DataContext.CurrentPerson.UserName"
RelativeSource="{RelativeSource AncestorType={x:Type Window}}"/>
<Binding Path="UserName"/>
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<ListBox ItemsSource="{Binding Persons}"
DisplayMemberPath="{Binding UserName}"
ItemContainerStyle="{StaticResource style}"
SelectedItem="{Binding SelectedPerson}">
</ListBox>
我做了一个和您一样的样式,但是我使用了一个多重绑定来传递要与转换器进行比较的值,而不是使用DataTrigger
在第一个绑定中,我在viewModel中检索当前用户的用户名,为此我需要指定对象的位置,这是relativeSource的原因
在第二个绑定中,我直接获取ListItemBox DataContext的属性UserName,它有一个Person bind类型的对象
就是这样,它像预期的那样工作
因为您无法编译它。对吗?当然。我知道这一点。我不知道我应该做什么。上面的例子说明了我想做什么,尽管这是胡说八道。;)你能详细说明一下吗?举个例子吧?非常感谢。它完全按照预期工作。我从未想过我可以使用绑定来设置值。再次感谢您的详细解释。:)