简体   繁体   English

WPF绑定多重触发条件空值

[英]Wpf bind multitrigger condition null value

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: DependencyProperty设置:

    /// <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: 表单xaml设置:

<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">

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: 您可能在Visual Studio的“输出”窗口中出现了类似以下内容的错误:

Error: No OverrideVisibility property found on object Window

Instead of that Binding , use the name/type of your custom Window ... something like this: 代替该Binding ,使用自定义 Window的名称/类型...类似这样:

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. 如果要监视DependencyProperty传递的值,则需要注册PropertyChangedCallback事件处理程序。 You can find out more from the Custom Dependency Properties page on MSDN. 您可以从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. 如果您可以在Style和视图都可以访问的程序集中声明一个附加属性,那么您仍然可以执行此操作。 If that's a possibility, then take a look at the Attached Properties Overview page on MSDN to find out how to do that. 如果可能的话,请查看MSDN上的“ 附加属性概述”页面,以了解如何执行此操作。

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. 此示例来自MSDN上的“ 属性路径语法”页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM