.net DataTrigger中的WPF自定义附加属性
我已经在这里查看了类似的问题,但未能找到解决方案,因此我的交易如下: **我有以下课程:**.net DataTrigger中的WPF自定义附加属性,.net,wpf,data-binding,attached-properties,.net,Wpf,Data Binding,Attached Properties,我已经在这里查看了类似的问题,但未能找到解决方案,因此我的交易如下: **我有以下课程:** public static class ControlSecurity { public static readonly DependencyProperty IsSecuredProperty = DependencyProperty.RegisterAttached( "IsSecured", typeof (bool),
public static class ControlSecurity
{
public static readonly DependencyProperty IsSecuredProperty =
DependencyProperty.RegisterAttached(
"IsSecured",
typeof (bool),
typeof (FrameworkElement),
new PropertyMetadata(false));
[AttachedPropertyBrowsableForType(typeof(Control))]
public static bool GetIsSecured(FrameworkElement ctl)
{
return (bool)ctl.GetValue(IsSecuredProperty);
}
public static void SetIsSecured(FrameworkElement ctl, bool value)
{
ctl.SetValue(IsSecuredProperty, value);
}
}
可以推测,它将安全性:ControlSecurity.issecuried
添加到所有框架元素中
注意:Security:
指向所有这些类所在的命名空间(包括ControlSecurity
)
因此,我为我的一个控件实现了此数据模板和样式:
<DataTemplate x:Key="SecureButtonTemplate">
<StackPanel Orientation="Horizontal">
<Image x:Name="SecureIcon" Source="pack://application:,,,/Resources/Icons/secure.png" Width="16" Height="16" Visibility="Collapsed" />
<ContentPresenter Content="{Binding}" />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}, Path=Security:ControlSecurity.IsSecured}" Value="true">
<Setter TargetName="SecureIcon" Property="Visibility" Value="Visible" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<Style TargetType="{x:Type Button}">
<Setter Property="ContentTemplate" Value="{StaticResource SecureButtonTemplate}" />
</Style>
想法是,我想找到父按钮,并绑定到它的Security:ControlSecurity.issecuried
我定义的附加属性
我在这个绑定上尝试了大约10种不同的变体,我不断地得到一个类似这样的绑定错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'Security:ControlSecurity' property not found on 'object' ''Button' (Name='')'. BindingExpression:Path=Security:ControlSecurity.IsSecured; DataItem='Button' (Name=''); target element is 'ContentPresenter' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data错误:40:BindingExpression路径错误:在“对象”“按钮”(名称=“”)上找不到“安全性:控制安全性”属性。BindingExpression:Path=Security:ControlSecurity.IsSecured;DataItem='Button'(名称='');目标元素是“ContentPresenter”(名称=“”);目标属性为“NoTarget”(类型为“Object”)
在这一点上,我很困惑,希望WPF大师能给我一些见解。只需添加括号:
Path=(Security:ControlSecurity.IsSecured)
好的,我这样做了,现在我得到一个XAML解析异常,内部异常声明:属性路径无效。”ControlSecurity“没有名为“IsSecured”的公共财产。忽略我的上述评论,我发现其中一个在我的附属财产中定义了错误的所有者。甜蜜的宗教信仰,这让我头疼。谢谢
Path=(Security:ControlSecurity.IsSecured)