1

我有一个简单的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使用名为Simple的自定义

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以下消息:简单从“初始”更改为“ ”

我怎样才能摆脱这种行为?

4

0 回答 0