Programmatically change the ListView selector style

I am making Addon for a Windows WPF application. Therefore, I can access the ListView using programming, but not edit the WPF source.

To add custom formatting for ListViewItem, depending on the data I created, my own class StyleSelectorand assigned an instance of it to the ListView property ItemContainerStyleSelector.

Here is the source:

  public class MySelector extends System.Windows.Controls.StyleSelector {

    private var oldSelector : System.Windows.Controls.StyleSelector;

    public function MySelector(oldSelector : StyleSelector, debug : Object) {
      this.oldSelector = oldSelector;
    }
    public function SelectStyle(item : Object, container : DependencyObject) : Style {
      if (this.oldSelector != null) {
        var oldStyle : System.Windows.Style = this.oldSelector.SelectStyle(item, container);
        if (item[3] == "3") {
          var newStyle : System.Windows.Style = new System.Windows.Style(oldStyle.TargetType, oldStyle);
          newStyle.Setters.Add(new Setter(Control.BackgroundProperty, System.Windows.Media.Brushes.Red));
          return newStyle;
        } else {
          return oldStyle;
        }
      }
      return null;
    }
  } 

This takes the old selector and adds a red background if the index of column 3 contains a value equal to "3".

This works fine, but when the row in the ListView hangs or is selected, the original style is still applied and the red background is lost until the row is selected or saved.

How can I apply my red background to these lines, even if they are selected or hang?

, XAML, . , -, JScript.NET.


newStyle :

          var t1 : Trigger = new Trigger();
          t1.Property = ListBoxItem.IsSelectedProperty;
          t1.Value = true;
          t1.Setters.Add(new Setter(Control.BackgroundProperty, System.Windows.Media.Brushes.Black));
          newStyle.Triggers.Add(t1);
          var t2 : Trigger = new Trigger();
          t2.Property = UIElement.IsMouseOverProperty;
          t2.Value = true;
          t2.Setters.Add(new Setter(Control.BackgroundProperty, System.Windows.Media.Brushes.Violet));
          newStyle.Triggers.Add(t2);
          var t3 : Trigger = new Trigger();
          t3.Property = UIElement.IsFocusedProperty;
          t3.Value = true;
          t3.Setters.Add(new Setter(Control.BackgroundProperty, System.Windows.Media.Brushes.Yellow));
          newStyle.Triggers.Add(t3);

.

+5
2

..

:

<Style x:Key="MenuButtonStyle" TargetType="Button">
    <Setter Property="Background" Value="#FF494646"/>
    <Setter Property="Foreground" Value="#FFE5E5E5"/>
    <Setter Property="TextOptions.TextFormattingMode" Value="Display"></Setter>
    <Setter Property="Cursor" Value="Hand"></Setter>
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect Opacity="0.7"/>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="Black"/>
        </Trigger>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property="Background" Value="ForestGreen"></Setter>
            <Setter Property="Foreground" Value="Black"/>
        </Trigger>
    </Style.Triggers>
</Style>
+3

All Articles