Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger for custom dependency properties in Style

Tags:

wpf

xaml

The problem

I defined a reusable control, MyControl, that extends a TextBox.

I want to set a Trigger to one of its dependency properties.
So I added a style to it, with Triggers.

But if I set the TargetType of the Style to MyControl, I get a XAML warning that 'MyControl' TargetType does not match type of element 'TextBlock'.
And if I set it to TextBlock, I get a compilation error that The member "MyDependencyProperty" is not recognized or is not accessible..

  • How can I define this style with the triggers?

Sample

C# code-behind

namespace UserControls.Local
{
    public partial class MyControl : TextBlock
    {
        #region Trogdor

        public static readonly DependencyProperty TrogdorProperty = DependencyProperty.Register(
            "Trogdor", typeof (bool), typeof (MyControl), new PropertyMetadata(default(bool)));

        public bool Trogdor
        {
            get { return (bool) GetValue(TrogdorProperty); }
            set { SetValue(TrogdorProperty, value); }
        }

        #endregion


        public MyControl()
        {
            InitializeComponent();
        }
    }
}

XAML

<TextBlock
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:UserControls.Local"
    mc:Ignorable="d"
    Text="BOOP!"
    x:Class="UserControls.Local.MyControl">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Foreground" Value="Blue"/>
            <Style.Triggers>
                <Trigger Property="Trogdor" Value="True">
                    <Setter Property="Foreground" Value="DeepPink"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
like image 533
ANeves Avatar asked Dec 14 '22 04:12

ANeves


2 Answers

The solution I found was to "fully qualify" the dependency property on the binding:

<Trigger Property="local:MyControl.Trogdor" Value="True">
like image 149
ANeves Avatar answered Dec 16 '22 17:12

ANeves


Not sure if you are still looking for a solution, but the answer in this thread worked for me, while yours didn't.

It uses a DataTrigger with a binding on the root element, instead of a Trigger:

<DataTrigger Binding="{Binding Path=Highlight, RelativeSource={RelativeSource AncestorType={x:Type Elements:DataElement}}}" Value="True">
    <Setter Property="Control.Background" Value="{DynamicResource EntryBoxHighlightBackground}"/>
</DataTrigger>

With your solution, I press the button and the value of the variable changes but the style trigger doesn't apply the changes, like it was not informed of the change in the variable.

like image 40
Luis Ferreira Avatar answered Dec 16 '22 17:12

Luis Ferreira