0

我拥有的是一个自定义窗口。添加了 bool 依赖属性。我想使用这个依赖属性作为我的触发器的条件。可以这么说,一种绕过我的触发器的方法。不幸的是,我抛出了正确的非空值异常。用这个敲我的头。我还在触发器上绑定之前测试了依赖属性。它永远不会碰到依赖属性包装器。当我这样做时没有抛出/显示错误。

依赖属性设置:

    /// <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);
        }
    }

风格的触发器设置

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

表单 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">
4

1 回答 1

1

您的错误在这一行:

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

具体来说,这是您的错误:

AncestorType={x:Type Window}

您可能在 Visual Studio 的输出窗口中出现错误,内容如下:

Error: No OverrideVisibility property found on object Window

取而代之的是Binding,使用您的自定义 Window名称/类型......像这样:

AncestorType={x:Type YourPrefix:YourCustomWindow}

另外,你这样说:

它永远不会碰到依赖属性包装器

它不会......它们仅供使用......它们被框架使用。如果要监视通过 a 的值DependencyProperty,则需要注册一个PropertyChangedCallback事件处理程序。您可以从 MSDN 上的自定义依赖属性页面了解更多信息。


更新>>>

啊,我才注意到评论。Style如果您可以在您和您的视图都可以访问的程序集中声明附加属性,您可能仍然可以这样做。如果有可能,请查看 MSDN 上的附加属性概述页面,了解如何执行此操作。

最后,您可以像这样绑定到附加属性:

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

此示例来自 MSDN 上的Property Path Syntax页面。

于 2014-03-25T23:08:54.690 回答