WPF:几种双向绑定相同 属性

WPF: Several two way bindings to same property

我的数据上下文中有一些 属性。

public string SomeProperty {get; set;}

我有两个(或更多)控件绑定到此 属性。例如:

<TextBox Text="{Binding SomeProperty, Mode=TwoWay}"></TextBox>
<TextBox Text="{Binding SomeProperty, Mode=TwoWay}"></TextBox>

这很好用,但我需要知道是什么控件改变了我的 属性。我能以某种方式做到这一点吗?

您无法在 属性 setter 内部分辨,但是 TextBox 控件确实有一个 SourceUpdated 事件可以与 NotifyOnSourceUpdated绑定属性.

在XAML:

<StackPanel>
    <TextBox x:Name="alpha"
        Text="{Binding SomeProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}"
        SourceUpdated="OnSourceUpdated" />
    <TextBox x:Name="bravo"
        Text="{Binding SomeProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}"
        SourceUpdated="OnSourceUpdated" />
</StackPanel>

在代码隐藏中:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public string SomeProperty
    {
        get
        {
            return _someProperty;
        }
        set
        {
            if (_someProperty != value)
            {
                _someProperty = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("SomeProperty"));
                }
            }
        }
    }
    string _someProperty;

    void OnSourceUpdated(object sender, DataTransferEventArgs e)
    {
        var textbox = (TextBox) sender;
        string message = string.Format(
            "Property value is '{0}' and was changed by TextBox '{1}'",
            SomeProperty, textbox.Name);
        Console.WriteLine(message);
    }
}

有两个 public 到同一个支持变量

private string someproperty
public string SomeProperty1 
{
     get { return someproperty; };
     set
     {
         if (someproperty == value) return;
         someproperty = value;
         NotifyPropertyChanged("SomeProperty1");
         NotifyPropertyChanged("SomeProperty2");
     }
}
public string SomeProperty2 
{
     get { return someproperty; };
     set
     {
         if (someproperty == value) return;
         someproperty = value;
         NotifyPropertyChanged("SomeProperty1");
         NotifyPropertyChanged("SomeProperty2");
     }
}

<TextBox Text="{Binding SomeProperty1, Mode=TwoWay}"></TextBox>
<TextBox Text="{Binding SomeProperty2, Mode=TwoWay}"></TextBox>

您可以使用 DependencyPropertyDescriptor:

    System.ComponentModel.DependencyPropertyDescriptor descriptor;
    public MainWindow()
    {
        InitializeComponent();

        descriptor = System.ComponentModel.DependencyPropertyDescriptor.FromProperty(TextBox.TextProperty, typeof(TextBox));
        descriptor.AddValueChanged(txtbox1, TxtChanged);
        descriptor.AddValueChanged(txtbox2, TxtChanged);

    }
    public void TxtChanged(object sender, EventArgs ea)
    {
        Debug.WriteLine("change from " + ((FrameworkElement)sender).Name);
    }