Question

I have a custom dependency property that I would like to use as a data trigger. Here is the code behind:

public static readonly DependencyProperty BioinsulatorScannedProperty =
    DependencyProperty.Register(
        "BioinsulatorScanned", 
        typeof(bool), 
        typeof(DisposablesDisplay), 
        new FrameworkPropertyMetadata(false));

    public bool BioinsulatorScanned
    {
        get
        {
            return (bool)GetValue(BioinsulatorScannedProperty);
        }
        set
        {
            SetValue(BioinsulatorScannedProperty, value);
        }
    }

I have created a style and control template. My goal is to change the color of some text when the dependency prop is set to true...

<Style x:Key="TreatEye" TargetType="Label">
        <Setter Property="Foreground" Value="#d1d1d1" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="FontSize" Value="30" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <Canvas>                            
                        <TextBlock x:Name="bioinsulatorText" 
                                   Canvas.Left="21" Canvas.Top="33" 
                                   Text="Bioinsulator" />
                        <TextBlock Canvas.Left="21" Canvas.Top="70" 
                                   Text="KXL Kit" />
                    </Canvas>

                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding BioinsulatorScanned}"
                                     Value="True">
                            <Setter TargetName="bioinsulatorText" 
                                    Property="Foreground" Value="Black" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>            
    </Style>

Despite successfully setting the dependency prop to true programmatically, This trigger condition never fires. This is a real pain to debug!

Thanks in advance.

Was it helpful?

Solution

It looks like your dependency property is defined inside a DisposableDisplay object that you created. In order for the binding specified to work, an instance of that DisposableDisplay object must be set as the DataContext of the control (label in this case) or any of its ancestors.

OTHER TIPS

In this case I am switching the visibility of a button using a datatrigger based on a dependency property FirstLevelProperty.

public static readonly DependencyProperty FirstLevelProperty = DependencyProperty.Register("FirstLevel", typeof(string), typeof(MyWindowClass));

public string FirstLevel
        {
            get
            {
                return this.GetValue(FirstLevelProperty).ToString();
            }

            set
            {
                this.SetValue(FirstLevelProperty, value);
            }
        }

You can reference the dependency property FirstLevel(Property) contained (in this case) in a window by using the RelativeSource binding. Also you should set the default setting in the style, that will be overridden by the datatrigger.

<Button.Style>
    <Style TargetType="Button">
        <Style.Triggers>
            <DataTrigger 
             Binding="{Binding Path=FirstLevel,
             RelativeSource={RelativeSource FindAncestor,
             AncestorType={x:Type Window}}}" 
             Value="SomeValue">
                <Setter Property="Visibility" 
                 Value="Hidden" />
             </DataTrigger>
        </Style.Triggers>
        <Setter Property="Visibility" Value="Visible" />
    </Style>
</Button.Style>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top