gpt4 book ai didi

windows - 通过ViewModel自定义UWP ListViewItem

转载 作者:行者123 更新时间:2023-12-03 10:26:27 27 4
gpt4 key购买 nike

寻求帮助。
是否有机会通过ViewModel {Binding}更改Listviewitem的ContentBorder:BorderBrush?
Something like this

最佳答案

更新了

您可以创建一个ItemContainerStyleSelector,您可以在其中基于列表中的当前项目应用自定义样式。

public class CustomItemContainerStyleSelector : StyleSelector
{
public Style MyStyle1 { get; set; }
public Style MyStyle2 { get; set; }

protected override Style SelectStyleCore(object item, DependencyObject container)
{
var obj = (MyObject)item;
if (/* Some kind of condition based on the bound object */)
{
return MyStyle1;
}
else
{
return MyStyle2;
}
}
}

您可以这样使用样式选择器:
<ListView ItemsSource="{Binding SomeList}">
<ListView.ItemContainerStyleSelector>
<local:CustomItemContainerStyleSelector>
<local:CustomItemContainerStyleSelector.MyStyle1>
<Style TargetType="ListViewItem">
<Setter Property="BorderThickness" Value="5,0,0,0"/>
<Setter Property="BorderBrush" Value="Red"/>
</Style>
</local:CustomItemContainerStyleSelector.MyStyle1>
<local:CustomItemContainerStyleSelector.MyStyle2>
<Style TargetType="ListViewItem">
<Setter Property="BorderThickness" Value="5,0,0,0"/>
<Setter Property="BorderBrush" Value="Yellow"/>
</Style>
</local:CustomItemContainerStyleSelector.MyStyle2>
</local:CustomItemContainerStyleSelector>
</ListView.ItemContainerStyleSelector>
</ListView>

关于windows - 通过ViewModel自定义UWP ListViewItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42583631/

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