通过ViewModel自定义UWP ListViewItem

时间:2017-03-03 16:14:19

标签: windows mvvm uwp win-universal-app mvvmcross

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

1 个答案:

答案 0 :(得分:1)

<强>已更新

您可以创建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>