How to define style triggers in user control

I have a user control and in it I have several textbox / calendar / ... controls that should all be enabled or disabled based on the properties in my user control (IsEditing)

I created the IsEditing dependency property in my user control, and now I want to define the style that all controls use to enable or disable it.

I am trying to do something like this:

<ad:DocumentContent.Resources> <Style x:Key="ReadOnlyMode"> <Setter Property="HorizontalAlignment" Value="Stretch"/> <Setter Property="VerticalAlignment" Value="Stretch"/> <Setter Property="Visibility" Value="Visible"/> <Setter Property="IsEnabled" Value="False" /> <Style.Triggers> <Trigger Property="IsEditing" Value="True"> <Setter Property="Background" Value="Green"/> </Trigger> </Style.Triggers> </Style> </ad:DocumentContent.Resources> 

But I get this error: the IsEditing property was not found in the type "FrameworkElement".

How can I access the correct property for triggers?


Update 1:

I defined IsEditing as follows:

 public static readonly DependencyProperty IsEditingProperty = DependencyProperty.Register( "IsEditing", typeof(Boolean), typeof(MyDetailDataControl), new PropertyMetadata(false)); 

And the following styles are defined:

 <ad:DocumentContent.Resources> <Style x:Key="ReadOnlyMode" xmlns:u="clr-namespace:MyProject.Controls" TargetType="{x:Type u:MyDetailDataControl}"> <Setter Property="HorizontalAlignment" Value="Stretch"/> <Setter Property="VerticalAlignment" Value="Stretch"/> <Setter Property="Visibility" Value="Visible"/> <Setter Property="IsEnabled" Value="False" /> <Style.Triggers> <Trigger Property="IsEditing" Value="True"> <Setter Property="Background" Value="Green"/> </Trigger> </Style.Triggers> </Style> </ad:DocumentContent.Resources> 

But now I get the following error: The property "IsEditing" was not found in the type "MyDetailDataControl".

I think there is a problem with defining a dependency property.


Update 2:

I changed xaml as follows (add xmlns: l = "clr-namespace: MyProject.Controls" in the user control section to make the namespace accessible everywhere), and also used the method defined here http://www.wpfmentor.com /2009/01/how-to-debug-triggers-using-trigger.html for debugging the launch)

 <ad:DocumentContent.Resources> <Style x:Key="ReadOnlyMode" TargetType="TextBox"> <Style.Triggers> <Trigger my:TriggerTracing.TriggerName="BoldWhenMouseIsOver" my:TriggerTracing.TraceEnabled="True" Property="l:MyDetailDataControl.IsEditing" Value="True"> <Setter Property="TextBox.Background" Value="Green"/> </Trigger> </Style.Triggers> </Style> </ad:DocumentContent.Resources> 

But does the trigger fail? Need to implement attribute notification mechanism? if so, how can i do this? Is there any special way to notify me when a property changes when the property is an attached property?


Update 3: Changed this version, which is not related to my own IsEditing property. But still does not work.

 <ad:DocumentContent.Resources> <Style x:Key="MyStyle" TargetType="{x:Type l:MyDetailDataControl}" > <Style.Triggers> <Trigger my:TriggerTracing.TriggerName="BoldWhenMouseIsOver" my:TriggerTracing.TraceEnabled="True" Property="IsMouseOver" Value="True"> <Setter Property="FontWeight" Value="Bold"/> </Trigger> </Style.Triggers> </Style> </ad:DocumentContent.Resources> 
+4
source share
1 answer

You need to set the TargetType style to your UserControl type. Or you are the prefix of all its properties.

 <!-- Using TargetType --> <Style xmlns:u="clr-namespace:Test.UserControls" x:Key="ReadOnlyMode" TargetType="{x:Type u:MyUserControl1}"> <Style.Triggers> <Trigger Property="IsEditing" Value="True"> <!-- ... --> </Trigger> </Style.Triggers> </Style> <!-- Using qualified property --> <Style xmlns:u="clr-namespace:Test.UserControls" x:Key="ReadOnlyMode"> <Style.Triggers> <Trigger Property="u:MyUserControl1.IsEditing" Value="True"> <!-- ... --> </Trigger> </Style.Triggers> </Style> 

As for your other problem: the property requires a CLR-Wrapper:

 public static readonly DependencyProperty IsEditingProperty = DependencyProperty.Register("IsEditing", typeof(Boolean), typeof(MyDetailDataControl), new UIPropertyMetadata(false)); // This: public Boolean IsEditing { get { return (Boolean)GetValue(IsEditingProperty); } set { SetValue(IsEditingProperty, value); } } 
+5
source

All Articles