私はここ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
)
そこで、コントロールの1つにこのデータテンプレートとスタイルを実装しました。
<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パスエラー:'Security:ControlSecurity'プロパティが'オブジェクト'''ボタン'(名前='')'に見つかりません。BindingExpression:Path = Security:ControlSecurity.IsSecured; DataItem ='ボタン'(名前=''); ターゲット要素は'ContentPresenter'(Name ='');です。ターゲットプロパティは「NoTarget」(タイプ「Object」)です
私はこの時点で困惑しており、そこにあるWPFの達人からの洞察が欲しいです。