2

I need to change the background of the selected item in a list view, programmatically.

The style currently looks like this (as default)

        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background">
                <Setter.Value>
                    <ImageBrush ImageSource="images/selection-large.png"/>
                </Setter.Value>
            </Setter>
        </Trigger>

I need to be able to change that background image of the selected item programmatically.

I have attempted to do this myself however i am not really sure how to go about it. I did notice that the listview has a styles property but other than that i am completely stumped.

I am using VB.net

Edit

Would something like this work? and if so, what am i doing wrong? My listview is completely invisible with this code.

    Dim trigger As New Trigger()
    trigger.Property = ListViewItem.IsSelectedProperty
    trigger.Value = True
    trigger.Setters.Add(New Setter(ListViewItem.BackgroundProperty, Brushes.Pink))
    mylistview.Style.Triggers.Add(trigger)
    mylistview.ItemContainerStyle = Style
4

3 回答 3

0

您可以在 XAML 中执行此操作,试试这个:

<Window.Resources>
    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <ImageBrush ImageSource="images/selection-large.png"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
于 2013-08-03T14:33:46.813 回答
0

我终于可以解决了。。我所要做的就是在第一个样式的基础上制作另一种样式,并给它们两个不同的 x:keys 。然后我就可以改变这样的样式了。

mylistview.ItemContainerStyle = DirectCast(FindResource("XKeyName"), Style)
于 2013-08-03T15:01:02.110 回答
0

将您的属性绑定IsSelected到 ViewModel 中的某个 bool 值并切换它。

<ListView.ItemContainerStyle>
   <Style TargetType="ListViewItem">
      <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
   </Style>
</ListView.ItemContainerStyle>

项目视图模型:

private bool _isSelected;
public bool IsSelected
{
    get { return _isSelected; }
    set 
    {
        _isSelected = value;
        NotifyPropertyChange("IsSelected");
    }
}

在 WPF 中,您无需在过程代码中操作 UI 元素,而是使用 DataBinding 并操作您的模型/视图模型的属性

顺便说一句,我建议不要对 WPF 中的常规 UI 元素使用 PNG 或其他基于位图的东西。

取而代之的是,您应该使用 WPF 元素创建您的 UI,这些元素是基于矢量的,因此与分辨率无关。

于 2013-08-03T13:36:15.170 回答