5

I am doing an Addon to a Windows WPF application. Therefore I can access the ListView with programming but not edit the WPF source.

To add special formatting for the ListViewItems depending on the data I created my own StyleSelector class and assigned an instance of it to the ListView ItemContainerStyleSelector property.

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 column index 3 contains a value equal to "3".

This works fine but when a row in the ListView is hovered or selected still the original style applies and the red background is lost until the line is un-selected or unhovered.

How can I apply my red background for those lines even when they are selected or hovered?

Remember, I cannot edit the XAML but programmatically access most properties. In case this is addon-code written in JScript.NET.


I have now tried to add triggers to newStyle with programming:

          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);

No effect.

Cœur
  • 37,241
  • 25
  • 195
  • 267
ZoolWay
  • 5,411
  • 6
  • 42
  • 76
  • Can you look at the triggers collection on your new style and remove the mouseover and hover triggers? `newStyle.Triggers` – Brent Stewart Mar 12 '13 at 15:12
  • Unfortunately oldStyle and newStyle both do not have any triggers. May they come from a theme? @BrentStewart – ZoolWay Mar 12 '13 at 15:47
  • These mouse hover and selection effects could be defined inside item template (which is DataTemplate). You first need to investigate and find out where the triggers are located. You can use, for example, ILSpy, to decompile xaml code from binaries – Woodman Mar 27 '13 at 14:34
  • Hmm, that is quite a good hint. I just checked. `ItemTemplate` and `ItemTemplateSelector` is `null`. But `Template` got something. Can that apply too? – ZoolWay Mar 28 '13 at 08:45

2 Answers2

3

I would use a resource library and manipulate the triggers etc there.

Get Started

Example:

<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>
  • Unfortunately I cannot add or modify the XAML but have tried to do so with programming, see updated question. But it did not work. – ZoolWay Mar 12 '13 at 15:51
  • This maybe a very unhelpful comment if so I apologise, but can you not get access due to your current limitations and problem? –  Mar 27 '13 at 15:21
  • Yes because my code is a "addon" product and through the interfaces I get the list but cannot change the compiled XAML. – ZoolWay Mar 28 '13 at 08:33
3

You may use StyleSelector mechanism in WPF.

http://msdn.microsoft.com/tr-tr/library/system.windows.controls.styleselector.aspx

http://www.shujaat.net/2010/10/wpf-style-selector-for-items-in.html

NthDeveloper
  • 969
  • 8
  • 16
  • Sorry but I already use `ItemContainerStyleSelector` as the question says. The Problem is, I can influence the style for *normal* rows but not for hovered or selected rows. – ZoolWay Mar 28 '13 at 08:36