これを行うにはいくつかの方法があります。非常にトリッキーなものを投稿します。警告これはwpf proのクレイジーな答えですが、うまくいきます。ハハハ...
public MainWindow()
{
InitializeComponent();
this.DataContext = new { D = "Hello" };
}
public static bool GetDoMyBinding(DependencyObject obj)
{
return (bool)obj.GetValue(DoMyBindingProperty);
}
public static void SetDoMyBinding(DependencyObject obj, bool value)
{
obj.SetValue(DoMyBindingProperty, value);
}
// Using a DependencyProperty as the backing store for DoMyBinding. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DoMyBindingProperty =
DependencyProperty.RegisterAttached("DoMyBinding", typeof(bool), typeof(MainWindow), new PropertyMetadata(false, new PropertyChangedCallback(OnDoMyBindingChanged)));
private static void OnDoMyBindingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Binding binding = new Binding("D");
binding.Mode = BindingMode.OneWay;
if (d is Button)
{
Button b = d as Button;
b.SetBinding(Button.ContentProperty, binding);
}
if (d is TextBlock)
{
TextBlock tb = d as TextBlock;
tb.SetBinding(TextBlock.TextProperty, binding);
}
}
XAML は次のようになります。
<Window x:Class="BindingTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:local="clr-namespace:BindingTest">
<StackPanel>
<Button local:MainWindow.DoMyBinding="True"/>
<Button local:MainWindow.DoMyBinding="True"/>
<TextBlock local:MainWindow.DoMyBinding="True" />
</StackPanel>
</Window>
結果は次のようになります。

編集済み: True の代わりに、".Operators.EditCommand" などのカスタム文字列を配置できますが、ロジックを変更する必要があります。
楽しむ。