Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a WPF Style Trigger to a custom dependency property

I have found numerous similar threads here, but none that seem to address my specific issue.

I need to highlight the background of a textbox under certain conditions. I have created a Highlight property and tried using a trigger in a style to set it but it doesn't actually ever highlight the text.

Here is my Style, simplified:

<Style x:Key="TextBoxStyle" BasedOn="{StaticResource CommonStyles}">
    <Style.Triggers>
        <Trigger Property="Elements:DataElement.Highlight" Value="True">
            <Setter Property="Control.Background"
                    Value="{DynamicResource EntryBoxHighlightBackground}"/>
        </Trigger>
    </Style.Triggers>
</Style>

Elements is defined as:

xmlns:Elements="clr-namespace:MDTCommon.Controls.Forms.Elements">

Then I have the section where the style is applied:

<!-- Applies above style to all TextBoxes -->
<Style TargetType="TextBox" BasedOn="{StaticResource TextBoxContentHolder}" >
    <Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
    <!-- Overrides the default Error Style -->
</Style>

In the code behind of the DataElement class is the following:

public static readonly DependencyProperty HighlightProperty = 
    DependencyProperty.Register("Highlight", typeof(bool), typeof(DataElement));

public bool Highlight
{
    get { return (bool)base.GetValue(HighlightProperty); }
    set { base.SetValue(HighlightProperty, value); }
}

A DataElement ultimately derived from UserControl and it contains a reference to TextBox object as well as othe objects.

In the CustomForm class that houses all of the DataElement objects I have the following to set the color.

Resources["EntryBoxHighlightBackground"] = Brushes.Yellow;

So, the first issue is that setting the Highlight property for the DataElement doesn't cause the textbox background to draw in yellow.

The other issue is that I realize that I am applying this style to all textboxes and I could have textboxes in other areas that are not actually contained within a DataElement, which may cause a binding issue.

like image 517
WPFNewbie Avatar asked Aug 01 '12 12:08

WPFNewbie


People also ask

What is custom dependency property in WPF?

Dependency properties are properties that are registered with the WPF property system through Register or RegisterReadOnly calls. The Register method returns a DependencyProperty instance that holds the registered name and characteristics of a dependency property.

Why dependency property is static in WPF?

DependencyProperty has to be static (Class level) because when we create multiple objects of the class which has that property and want to refer the default value for that property the value has to come from that static instance of DependencyProperty.

What is DataTrigger in WPF?

A DataTrigger allows you to set property values when the property value of the data object matches a specified Value. For example, if you are displaying a list of Employee objects, you may want the foreground color to be different based on each Employee's current attendance.


1 Answers

Try converting your trigger to a DataTrigger, and add a binding that will look directly at the DataElement control, like so:

<DataTrigger Binding="{Binding Path=Highlight, RelativeSource={RelativeSource AncestorType={x:Type Elements:DataElement}}}" Value="True">
    <Setter Property="Control.Background" Value="{DynamicResource EntryBoxHighlightBackground}"/>
</DataTrigger>
like image 111
WPFNewbie Wannabe Avatar answered Oct 13 '22 22:10

WPFNewbie Wannabe