当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - WPF: Several two way bindings to same property

问题描述:

I have some property in my data context.

public string SomeProperty {get; set;}

And I have two (or more) controls binded to this property. For example:

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

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

This works fine, but I need to know what control have changed my property. Can I do this somehow?

网友答案:

Have two public to the same backing variable

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>
网友答案:

You can't tell inside your property setter, but the TextBox control does have a SourceUpdated event that can be used in conjunction with the NotifyOnSourceUpdated binding property.

In the 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>

In the code-behind:

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);
    }
}
网友答案:

You could use 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);
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: