2

我已经在 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的 s。

注意: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 错误:40:BindingExpression 路径错误:在 'object' ''Button' (Name='')' 上找不到 'Security:ControlSecurity' 属性。BindingExpression:Path=Security:ControlSecurity.IsSecured; 数据项='按钮'(名称='');目标元素是'ContentPresenter'(名称='');目标属性是“NoTarget”(类型“对象”)

在这一点上我很难过,并且希望从那里的 WPF 专家那里获得一些见解。

4

1 回答 1

11

只需添加括号:

Path=(Security:ControlSecurity.IsSecured)
于 2010-08-18T16:39:00.623 回答