Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to define triggers in style in user control

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

I created a dependency property IsEditing in my user control and now I want to define a style that all of the controls use it to became enabled or disabled.

I am trying to do something such as 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 am getting this error: Property 'IsEditing' was not found in type 'FrameworkElement'.

How can I address the correct property for triggers?


Update 1:

I defined the IsEditing as follow:

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

And defined styles as follow:

<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 I am getting the following error now: Property 'IsEditing' was not found in type 'MyDetailDataControl'.

I think there is a problem with the dependency property definition.


Update 2:

I changed the xaml as follow (add xmlns:l="clr-namespace:MyProject.Controls" at the user control section so the namespace became available everywhere) and also used the method which is define here http://www.wpfmentor.com/2009/01/how-to-debug-triggers-using-trigger.html to debug the trigger)

<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 the trigger is not fired? Do you I need implement notify property mechanism? if yes, how can I do this? Is there any special technique for implementing property change notification when the property is an attached property?


Update 3: Changed to this version, which is not related to my own IsEditing property. But still doesn't 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>
like image 970
mans Avatar asked Jul 04 '11 12:07

mans


1 Answers

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

<!-- 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>

Regarding your other problem: The property needs 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); }
}
like image 110
H.B. Avatar answered Oct 18 '22 01:10

H.B.