Using trigger to change property of many items - c#

I created a WPF custom control, which works fine.
It has a style which sets some properties and a template.
Now i want to change the control, so it has a 'Active' property.
If this is true it should use the Property 'ActiveBrush' for the Stroke
of some Rectangles in the Template, else it should use 'InactiveBrush'.
I want to use the ActiveBrush as the default Stroke, and change it to InactiveBrush
with a Trigger.
This works fine with one Rectangle when i use this:
<Trigger Property="Active" Value="False">
<Setter TargetName="Rec1" Property="Stroke" Value="{Binding Path=InactiveBrush, RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
But since I don't want to set each rectangle with a seperated setter, I am asking myself if it shouldn't be possible to set the property of all Rectangles in the Template with one Setter.
I already tried:
<Trigger Property="Active" Value="False">
<Setter Property="Rectangle.Stroke" Value="{Binding Path=InactiveBrush, RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
But this didn't work.
Has anyone a suggestion, how to implement this?
Thanks in advance.

#Robert Rossney - this style will not run since a target type of Rectangle doesn't have a property Active. But that is the right path to go, with a minor change:
<Style TargetType="Rectangle">
<Style.Triggers>
<DataTrigger Binding="{Binding Active, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyControl}}}" Value="False">
<Setter Property="Stroke" Value="{Binding Path=InactiveBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyControl}}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
This style should be nested in Style.Resources of the control style or in ControlTemplate.Resources of the ControlTemplate for the control. This way you're localizing this style only to your control. Any Rectangle outside of your control will not be affected.

I'm sure you know this but are just overlooking it: If you want to apply a style to all controls of a type, create a style with a TargetType of that type and put it in your custom control's resource dictionary. If you still need to apply specific styles to individual controls of that type, define those styles using the BasedOn property.
So, in your MyControl.Resources element, you'd put:
<Style TargetType="Rectangle">
<Style.Triggers>
<Trigger Property="Active" Value="False">
<Setter Property="Stroke" Value="{Binding Path=InactiveBrush, RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
<Style>
<Style>
and any Rectangle that needed its own style would start like this:
<Style TargetType="Rectangle" BasedOn="{StaticResource {x:Type Rectangle}}">

Related

WPF DataTrigger Based on Color

I'm trying to change the background of control A based on the background of control B, using a DataTrigger.
I've attempted to do this the conventional way you would change any property in a DataTrigger. Worst case I figure I can just use a converter, but wanted to try this first. Where have I gone astray, or is there simply no way to do this (i.e., bind to a control's background in a DataTrigger)?
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Style.Triggers>
<DataTrigger Binding="{Binding Background, ElementName=Icon}" Value="Black">
<Setter Property="Background" Value="LimeGreen"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>

Set Background of button based on Content or CustomDP value

I have a button whose Content property keeps changing based on certain conditions which are set from the ViewModel. Essentially it gets set a value from 0 to 1000.
I also have another custom DP property on the same button whose value can be set to a certain enum.
What I am trying to solve is as follows (and unfortunately have no idea how I should go about it):
If my button content value is 0 - background of the button should be Gray.
If my button content value is 1 - background of the button should be Yellow.
If my button content value is 1 & the Custom DP has a value set (not the default value) - background should change to Red.
If my problem statement was just dealing with setting a background based on the integer set on the content - I could easily use Converters (String to Brush) and set my background. But the last condition which is now setting my background based on custom DP value coming in - have no idea how to solve...
Can somebody please suggest me an approach - sample code to solve such a problem.
<Button Content="0">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=Content}" Value="0">
<Setter Property="Background" Value="Gray"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=Content}" Value="1">
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Value="1"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=YourDP}" Value="YourValue"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="Red"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
You can use multi-conditions triggers or multi data triggers. See this link and this link.

WPF DataTrigger binding if parent is typeof

I have a global scrollbar style for my application but I want the scrollbars to appear slightly different if they appear anywhere inside a specific type of control that has its IsActive property set to true. The code below works but I get a binding error every time the scrollbars aren't hosted in the custom control. Is there a way to check the ancestory before trying to set the trigger?
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=docking:DockingWindow}, Path=IsActive}" Value="False">
<Setter TargetName="Background" Property="Fill" Value="{DynamicResource Gray130}" />
</DataTrigger>

Deriving (Expanding) a Control Template in WPF

I have this DataGrid I want to change the Disabled behaviour (xaml);
I want to change this small part in the template.
If it is not possible i dont mind to use:
<Setter Property="OverridesDefaultStyle" Value="True"/>
And To replace the entire xaml (template) of my control, But I need the complete template to copy paste and help where to change the Disabled look-like part.
Can anyone help me?
EDIT: It's been pointed out that all the default control templates are available at MSDN which makes the below relevant, but I'll leave it here for interest.
Given an instance of a control you get serialize the markup for the control template using the System.Windows.Markup.XamlWriter class.
To get a control template:
string markup = System.Windows.Markup.XamlWriter.Save(control.Template);
To get a complete dump (including triggers etc) of the control template use.
StringBuilder markupBuilder = new StringBuilder();
XmlWriter writer = XmlWriter.Create(markupBuilder);
System.Windows.Markup.XamlDesignerSerializationManager manager =
new System.Windows.Markup.XamlDesignerSerializationManager(writer);
manager.XamlWriterMode = System.Windows.Markup.XamlWriterMode.Value;
// data grid named dataGrid1
var template = dataGrid1.Template;
System.Windows.Markup.XamlWriter.Save(dataGrid1.Template, manager);
string markup = markupBuilder.ToString();
If you just looking to change the foreground color of the DataGrid when it's disabled, you should be able to use styles together with triggers rather than replacing the entire template.
<DataGrid>
<DataGrid.Resources>
<Style
TargetType="{x:Type DataGridColumnHeader}">
<Style.Triggers>
<DataTrigger
Binding="{Binding IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
Value="False">
<Setter
Property="Foreground"
Value="Green" />
</DataTrigger>
</Style.Triggers>
</Style>
<Style
TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<DataTrigger
Binding="{Binding IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
Value="False">
<Setter
Property="Foreground"
Value="Green" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<!-- Column Definitions -->
</DataGrid>
Adding the above 2 styles to the DataGrid's resources collection will, set the foreground of each column header and data row cell to green when the DataGrid is disabled.
To define the disabled behavior of any control, you should change the Disabled visual state accordingly in the control template.

Trigger on ContextMenu.IsOpen in XAML

Here's what I'm trying to do:
<Style x:Key="TreeViewItemStyle">
<Setter Property="TreeViewItem.ContextMenu" Value="{StaticResource ContextMenu}" />
<Style.Triggers>
<Trigger Property="TreeViewItem.ContextMenu.IsOpen" Value="True">
<Setter Property="TreeViewItem.BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect GlowColor="Yellow" GlowSize="2"/>
</Setter.Value>
</Setter>
</Trigger>
</Style>
...
But it is obviously not working because Property="TreeViewItem.ContextMenu.IsOpen" is not recognized. Any suggestions to what I need to change?
You can bind to the IsOpened property of the context menu using a DataTrigger:
<DataTrigger Binding="{Binding ContextMenu.IsOpen, RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
Unfortunately, since all of the items in TreeView share the same ContextMenu, that will highlight all of them at once. There doesn't seem to be a property that lets you find out which FrameworkElement opened the ContextMenu.
You could handle the ContextMenuOpening and ContextMenuClosing events on the TreeViewItem, since those will bubble up from the control that handled the click and pass through the right TreeViewItem. If you want to do it in XAML, you could use an EventTrigger to start a one-frame animation that changes your property. The cleanest option may be to write an attached behavior that handles the ContextMenuOpening and ContextMenuClosing events and sets an attached property to true when the context menu is open.

Categories