Bradley Odell
Bradley Odell

Reputation: 1258

DataTrigger binding to UserControl dependency property

I'm having trouble using a DataTrigger to change a UserControl visually when it is "selected".
I have created a 'bool' dependency property in the class called "IsSelected".

I am using this XAML:

<UserControl x:Name="zControl"
             x:Class="Intel.AdaptivePerformance.Client.UI.Controls.ZoomControl"
             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" 
             mc:Ignorable="d" 
             d:DesignWidth="50" d:DesignHeight="32">
    <Border BorderBrush="Black" BorderThickness="1,0,1,2" Background="#2F000000">
        <Border.Style>
            <Style TargetType="{x:Type Border}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=zControl, Path=IsSelected}" Value="True">
                        <Setter Property="BorderThickness" Value="1,0,1,0" />
                        <Setter Property="Background" Value="{x:Null}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
        <Label Content="{Binding ElementName=zControl, Mode=OneWay, Path=Text}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" />
    </Border>
</UserControl>

And using this C#:

public partial class ZoomControl : UserControl {

    public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(ZoomControl), new PropertyMetadata(false));
    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ZoomControl), new PropertyMetadata(null));

    public ZoomControl() {
        InitializeComponent();
    }

    public bool IsSelected {
        get {
            return (bool) GetValue(IsSelectedProperty);
        }
        set {
            SetValue(IsSelectedProperty, value);
        }
    }

    public string Text {
        get {
            return (string) GetValue(TextProperty);
        }
        set {
            SetValue(TextProperty, value);
        }
    }

}

It's strange because the binding to the Label's Content property is working but the binding to the DataTrigger is not triggering. I have tried adding Mode=OneWay to the DataTrigger binding but it doesn't make a difference.

Edit:

I added a different Setter property "Margin" and that property worked.
So it appears that, for some reason, the Setters aren't able to override the existing properties of the Border. Anyone know why?

Upvotes: 3

Views: 7155

Answers (1)

user2250152
user2250152

Reputation: 20625

Explanation is here: http://msdn.microsoft.com/en-us/library/ms743230(v=vs.110).aspx#multiple_sets Local value takes precedence over trigger.
So try to change this:

<Border BorderBrush="Black" BorderThickness="1,0,1,2" Background="#2F000000">
    <Border.Style>
        <Style TargetType="{x:Type Border}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=zControl, Path=IsSelected}" Value="True">
                    <Setter Property="BorderThickness" Value="1,0,1,0" />
                    <Setter Property="Background" Value="{x:Null}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
    <Label Content="{Binding ElementName=zControl, Mode=OneWay, Path=Text}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" />
</Border>

to:

<Border BorderBrush="Black">
    <Border.Style>
        <Style TargetType="{x:Type Border}">
            <Setter Property="Background" Value="#2F000000" />
            <Setter Property="BorderThickness" Value="1,0,1,2" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=zControl, Path=IsSelected}" Value="True">
                    <Setter Property="BorderThickness" Value="1,0,1,0" />
                    <Setter Property="Background" Value="{x:Null}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
    <Label Content="{Binding ElementName=zControl, Mode=OneWay, Path=Text}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" />
</Border>

Upvotes: 9

Related Questions