2014-03-25 43 views
0

我有一个自定义窗口。添加了一个布尔依赖属性。我想使用这个依赖属性作为我的触发条件。可以这么说来绕过我的触发器。不幸的是,我有propery抛出非空值异常。用这一个刺激我的头。我还在绑定触发器之前测试了依赖项属性。它从不碰到依赖属性包装器。当我这样做时,不会抛出/显示任何错误。Wpf绑定multitrigger条件空值

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

触发设置在风格

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

不幸的是,我不能确切地说。也许在这里设置类型的本地:MyWindow的:“<条件绑定=”{Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:类型local:MyWindow}},Path = OverrideVisibility}“Value =”false“/> 。 –

+0

不幸的是,我有它的结构。带有触发器的样式位于另一个名称空间的资源字典中,在该字典中,我无法包含自定义窗口的名称空间,这就是为什么我使用类型窗口的查找祖先。它适用于我的其他依赖属性。 – user892381

回答

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} 

另外,你说的这个:

It never hits the dependency property wrapper

不会......它们只是使用...他们使用的框架。如果您想要监视DependencyProperty的值是什么,那么您需要注册一个PropertyChangedCallback事件处理程序。您可以从MSDN上的Custom Dependency Properties页面了解更多信息。


UPDATE >>>

啊,我只注意到了意见。如果您可以在程序集中声明附加属性,并且您的视图和您的视图都有权访问,那么您仍然可以执行此操作。如果这是一种可能性,那么请查看MSDN上的Attached Properties Overview页面,了解如何做到这一点。

最后,你可以绑定到一个像这样的附加属性:

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

这个例子是从Property Path Syntax页面上MSDN。