我在SO上看过类似的问题并且无法得到解决方案,所以这是我的交易:
**我有以下课程:**
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);
}
}
正如您所猜测的那样,它会为所有Security:ControlSecurity.IsSecured
添加FrameworkElement
。
注意: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>
这里的问题在于DataTrigger
:
{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}, Path=Security:ControlSecurity.IsSecured}
我的想法是,我想找到父按钮,然后绑定到我定义的Security:ControlSecurity.IsSecured
附加属性。
我在这个绑定上尝试了大约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')
我在这一点上很难过,并且非常喜欢WPF专家的一些见解。
答案 0 :(得分:11)
只需添加括号:
Path=(Security:ControlSecurity.IsSecured)