wpf NotifySourceUpdated 和 Binding.SourceUpdated 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13024924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NotifySourceUpdated & Binding.SourceUpdated not working
提问by WPF-it
My event below (OnSourceUpdated) is not getting handled.
我在 ( OnSourceUpdated)下面的事件没有得到处理。
XAML:
XAML:
<StackPanel x:Name="MyStackPanel"
Orientation="Horizontal"
DockPanel.Dock="Top">
<TextBox Text="{Binding Side, Mode=TwoWay}"
Width="100"/>
<TextBlock Background="Yellow"
Text="{Binding Side, Mode=OneWay,
NotifyOnSourceUpdated=True}"
Width="100"
SourceUpdated="OnSourceUpdated"
Binding.SourceUpdated="OnSourceUpdated"/>
</StackPanel>
C#:
C#:
....
MyStackPanel.DataContext = new MyItemClass() { Side = "Test" };
....
private void OnSourceUpdated(Object sender, DataTransferEventArgs args)
{
var i = args.Property;
}
public class MyItemClass : INotifyPropertyChanged
{
private string _side;
public string Side
{
get { return _side; }
set
{
_side = value;
OnPropertyChanged("Side");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
I have all the relevant settings done like NotifyOnSourceUpdated& SourceUpdated& Binding.SourceUpdatedetc.
我已经完成了所有相关设置,例如NotifyOnSourceUpdated& SourceUpdated&Binding.SourceUpdated等。
回答by Klaus78
From msdn: Binding.SourceUpdatedattached event occurs when a value is transferred from the binding target to the binding source, but only for bindings with the NotifyOnSourceUpdated value set to true
来自msdn:当值从绑定目标传输到绑定源时,会发生Binding.SourceUpdated附加事件,但仅适用于将 NotifyOnSourceUpdated 值设置为 true 的绑定
In the Binding of TextBlock, there is no value transfer from the binding target (TextBlock.Text) to the binding source (Side). Thus SourceUpdated cannot be fired.
在 TextBlock 的 Binding 中,没有从绑定目标 ( TextBlock.Text) 到绑定源 ( Side) 的值传递。因此无法触发 SourceUpdated。
Instead SourceUpdated can be fired on the first binding. Indeed here the target binding TextBox.Textcan change the binding source (Side).
相反,可以在第一次绑定时触发 SourceUpdated。实际上,这里的目标绑定TextBox.Text可以更改绑定源 ( Side)。
回答by FanerYedermann
Maybe I'm missing something, but I'm thinking your approach to updating is a bit strange. Is there a reason you're not just going with
也许我遗漏了一些东西,但我认为您的更新方法有点奇怪。有没有理由你不只是去
<TextBlock Text="{Binding foo, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ... />
If you're just interested in updates coming from source, that's normally the way of doing it. Calling
如果您只对来自源代码的更新感兴趣,通常就是这样做的。打电话
OnPropertyChanged( "PropertyName" )
covers the rest.
涵盖其余部分。

