Are*_*ren 2 .net data-binding wpf attached-properties
我在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);
}
}
Run Code Online (Sandbox Code Playgroud)
你可以猜测,它增加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>
Run Code Online (Sandbox Code Playgroud)
这里的问题在于DataTrigger:
{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}, Path=Security:ControlSecurity.IsSecured}
Run Code Online (Sandbox Code Playgroud)
我的想法是,我想找到父按钮,并绑定到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大师们的一些见解.
Tho*_*que 11
只需添加括号:
Path=(Security:ControlSecurity.IsSecured)
Run Code Online (Sandbox Code Playgroud)