我有一个简单的Window
<Window x:Class="BindingProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Name="mainWnd">
<TextBox Text="{Binding Path=Simple, ElementName=mainWnd, Mode=OneWayToSource, UpdateSourceTrigger=Explicit}"/>
</Window>
使用名为简单
的自定义DependencyProperty
public static DependencyProperty SimpleProperty = DependencyProperty.Register("Simple", typeof(string), typeof(MainWindow), new PropertyMetadata("initial", SimpleChanged));
private static void SimpleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MessageBox.Show(string.Format("Simple changed from \"{0}\" to \"{1}\"", e.OldValue.ToString(), e.NewValue.ToString()));
}
public string Simple
{
get { return (string)GetValue(SimpleProperty); }
set { SetValue(SimpleProperty, value); }
}
问题是当在应用程序启动时第一次设置绑定时,即使没有调用UpdateSource
,我也会更新源(简单属性的值)请参阅MessageBox
,并附上以下消息:简单地从“初始”更改为“”。
我怎样摆脱这种行为?