Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF:对同一属性的多个双向绑定_C#_Wpf_Data Binding - Fatal编程技术网

C# WPF:对同一属性的多个双向绑定

C# WPF:对同一属性的多个双向绑定,c#,wpf,data-binding,C#,Wpf,Data Binding,我的数据上下文中有一些属性 public string SomeProperty {get; set;} 我有两个(或更多)控件绑定到此属性。例如: <TextBox Text="{Binding SomeProperty, Mode=TwoWay}"></TextBox> <TextBox Text="{Binding SomeProperty, Mode=TwoWay}"></TextBox> 这很好,但我需要知道是什么控制改变了我的属

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

public string SomeProperty {get; set;}
我有两个(或更多)控件绑定到此属性。例如:

<TextBox Text="{Binding SomeProperty, Mode=TwoWay}"></TextBox>
<TextBox Text="{Binding SomeProperty, Mode=TwoWay}"></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>

将两个公共变量设置为同一支持变量

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>
私有字符串属性
公共字符串SomeProperty1
{
获取{return someproperty;};
设置
{
如果(someproperty==value)返回;
someproperty=值;
通知财产变更(“某些财产1”);
NotifyPropertyChanged(“SomeProperty2”);
}
}
公共字符串SomeProperty2
{
获取{return someproperty;};
设置
{
如果(someproperty==value)返回;
someproperty=值;
通知财产变更(“某些财产1”);
NotifyPropertyChanged(“SomeProperty2”);
}
}

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

尽管这很有效,但它的伸缩性并不好。OP特别提到了“两个(或更多)控制”(强调我的控制)。也就是说,这是在ViewModel中处理它而不使用事件的唯一方法。@Steven询问您认为一个窗口上会有多少个文本框引用相同的值?从性能和资源的角度来看,它的可扩展性很好。与您的回答不同,它确定了哪个控件“更改了我的属性”。我的回答很好,非常感谢,您是否愿意告诉我们为什么不更改?所述问题是属性。在您的回答中,属性设置器不知道是哪个控件更改了它。这不是OP的要求:“我需要知道哪些控件更改了我的属性”没有提到必须在ViewModel中执行此操作。我们的答案是做同一件事的不同方法,考虑到原始问题中所述的要求,这两种方法都是同样正确的。只是一个观察:如果您使用MVVM,ViewModel不应该真正了解其视图,事实上,您应该能够在不影响ViewModel行为的情况下切换视图。只要你的虚拟机“需要知道什么控件改变了我的属性”,那么你就已经有效地将你的虚拟机绑定到了一个特定的视图,在我看来,这不是真正的“MVVM方式”。是的,你是对的,这不是MVVM方式。我不会根据此信息执行任何功能。我只需要找出哪个控件触发了我的setter。我想知道在WPF中是否有这样做的方法。三个答案中的任何一个(在撰写本文时)都应该有效@尽管您在问题中没有明确说明这是一项要求,但只有BARM允许您在属性设置器本身中检测更改。我建议尝试这三种解决方案,然后投票表决,接受你认为解决问题的答案。
    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);
    }