WPF:对同一属性的几种双向绑定

时间:2015-01-23 15:12:44

标签: c# wpf data-binding

我的数据环境中有一些属性。

public string SomeProperty {get; set;}

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

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

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

3 个答案:

答案 0 :(得分:2)

对同一支持变量

有两个公开
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>

答案 1 :(得分:0)

您无法告诉属性设置器,但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);
    }
}

答案 2 :(得分:0)

您可以使用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);
    }