Wpf bind multitrigger condition null value

1.7k Views Asked by At

What I have is a custom window. Added a bool dependencyproperty. I want to use this dependency property as a condition for my triggers. A way to get around my triggers so to speak. Unfortunately I have propery non-null value exception being thrown. Banging my head with this one. I also tested the dependency property before the binding on the triggers. It never hits the dependency property wrapper. No errors thrown/shown when I do that.

DependencyProperty setup:

    /// <summary>
    /// The override visibility property
    /// </summary>
    public static readonly DependencyProperty OverrideVisibilityProperty = DependencyProperty.Register(
        "OverrideVisibility", typeof(bool), typeof(MyWindow), new PropertyMetadata(false));

    /// <summary>
    /// Gets or sets the override visibility.
    /// </summary>
    /// <value>The override visibility.</value>
    public bool OverrideVisibility
    {
        get
        {
            return (bool)this.GetValue(OverrideVisibilityProperty);
        }

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

Trigger setup in style

               <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="WindowStyle" Value="None" />
                            <Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=OverrideVisibility}" Value="false" />  
                        </MultiTrigger.Conditions>
                        <MultiTrigger.Setters>
                            <Setter TargetName="WindowCloseButton" Property="Visibility" Value="Visible" />
                        </MultiTrigger.Setters>
                    </MultiTrigger>
               </ControlTemplate.Triggers>

Form xaml Setup:

<local:MyWindow x:Class="MyForm"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    Width="500"
                    Height="500"
                    OverrideVisibility="True">
1

There are 1 best solutions below

0
On

Your error is on this line:

<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, 
    AncestorType={x:Type Window}}, Path=OverrideVisibility}" Value="false" />  

Specifically, this is your error:

AncestorType={x:Type Window}

You probably have an error in your Output Window in Visual Studio that says something like:

Error: No OverrideVisibility property found on object Window

Instead of that Binding, use the name/type of your custom Window... something like this:

AncestorType={x:Type YourPrefix:YourCustomWindow}

Additionally, you said this:

It never hits the dependency property wrapper

It wouldn't... they are just for your use... they're not used by the Framework. If you want to monitor what values are going through a DependencyProperty, then you need to register a PropertyChangedCallback event handler. You can find out more from the Custom Dependency Properties page on MSDN.


UPDATE >>>

Ah, I just noticed the comments. You might still be able to do it if you can declare an Attached Property in an assembly that both your Style and your view have access to. If that's a possibility, then take a look at the Attached Properties Overview page on MSDN to find out how to do that.

Finally, you can bind to an Attached Property like this:

<animation Storyboard.TargetProperty="(ownerType.propertyName)" .../>

This example was from the Property Path Syntax page on MSDN.