wpf中的2路绑定调用属性两次

Ric*_*nks 4 c# data-binding wpf xaml

我一直在玩wpf和2路数据绑定以更好地理解它,并且我注意到当文本框有2路数据绑定到属性时,属性被调用两次.我通过在调用属性时将值写入输出窗口来验证这一点.我的代码如下: -

我的xaml

<Page
    x:Class="_2waybindTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:_2waybindTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <TextBox HorizontalAlignment="Left" Margin="55,93,0,0" TextWrapping="Wrap" Text="{Binding TestProperty, Mode=TwoWay}" VerticalAlignment="Top" Width="540"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="55,31,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>
        <TextBox HorizontalAlignment="Left" Margin="55,154,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="540"/>
    </Grid>
</Page>
Run Code Online (Sandbox Code Playgroud)

我要测试的简单viewmodel类

public class viewmodel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _TestProperty;

    public void SetTestProperty()
    {
        this.TestProperty = "Set Test Property";
    }

    public string TestProperty{
        get
        {
            return this._TestProperty;
        }
        set
        {
            this._TestProperty = value;

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("TestProperty"));
            }

            Debug.WriteLine("this._TestProperty = " + this._TestProperty);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的xaml代码背后

/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        DataContext = new viewmodel();
    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        var vm = (viewmodel)DataContext;
        vm.SetTestProperty();
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么要调用两次.这是预期的行为吗?

ale*_*lex 8

通常,您应该在触发propertyChanged事件之前检查值是否实际更改,否则您可能会进入绑定更新的infinte周期.在您的情况下,文本框可能正在检查更改,以防止此类循环.

public string TestProperty{
    set
    {
        if(this._TestProperty == value)
        {
            return;
        }

        this._TestProperty = value;

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("TestProperty"));
        }            
    }
}
Run Code Online (Sandbox Code Playgroud)