我所拥有的是一个自定义窗口。添加了bool dependencyproperty。我想使用此依赖项属性作为我的触发器的条件。一种绕过我的触发器的方法可以这么说。不幸的是,我抛出了非空值异常。用这个敲打我的脑袋。我还在绑定触发器之前测试了依赖属性。它永远不会遇到依赖属性包装器。当我这样做时,没有错误抛出/显示。
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 :(得分: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}
另外,你说过这个:
它永远不会遇到依赖属性包装器
它不会......它们只是用于你的使用......它们不是框架使用的。如果要监视DependencyProperty
中的值,则需要注册PropertyChangedCallback
事件处理程序。您可以在MSDN上的Custom Dependency Properties页面上找到更多信息。
更新&gt;&gt;&gt;
啊,我刚刚注意到这些评论。如果您可以在Style
和您的视图都可以访问的程序集中声明附加属性,那么您仍然可以执行此操作。如果有可能,请查看MSDN上的Attached Properties Overview页面,了解如何执行此操作。
最后,您可以绑定到这样的附加属性:
<animation Storyboard.TargetProperty="(ownerType.propertyName)" .../>
此示例来自MSDN上的Property Path Syntax页。