寻求帮助。
是否有机会通过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/