gpt4 book ai didi

c# - 如何根据行对象是否是 viewModel 上可用的另一个集合的一部分来更改 GridView 行背景

转载 作者:行者123 更新时间:2023-12-03 11:03:33 25 4
gpt4 key购买 nike

我有基本的gridView充满了列。但是,我想更改 背景 基于如果在 viewModel 上声明的另一个集合中找到对象这一事实, ,除了 itemsSourcegridView .

有什么建议我怎么能做到这一点?

我调查了 IValue 转换器 但在这种情况下,我需要访问 viewModel从转换器收集或数据库,我认为这不是一件好事?

I am using .NET 4.5, WPF

最佳答案

您可以使用 IMultiValueConverter 并绑定(bind)到对象本身和其他集合,例如:

<ListView x:Name="lv">
<ListView.Resources>
<local:TheConverter x:Key="TheConverter" />
</ListView.Resources>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource TheConverter}">
<Binding Path="." />
<Binding Path="DataContext.TheOtherCollectionProperty" RelativeSource="{RelativeSource AncestorType=ListView}" />
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}" />
</GridView>
</ListView.View>
</ListView>
public class TheConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null || values.Length < 2)
return false;

YourDataObject obj = values[0] as YourDataObject;
System.Collections.IList collection = values[1] as System.Collections.IList;

return collection != null && collection.Contains(obj);
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

关于c# - 如何根据行对象是否是 viewModel 上可用的另一个集合的一部分来更改 GridView 行背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41593035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com