Reputation: 5505
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 ListViewItem
s 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.
Upvotes: 5
Views: 2688
Reputation: 1009
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
Upvotes: 3
Reputation:
I would use a resource library and manipulate the triggers etc there.
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>
Upvotes: 3