c# - wpf 中的 2 种方式绑定(bind)两次调用属性

标签 c# wpf xaml data-binding

我一直在玩 wpf 和 2 方式数据绑定(bind)以更好地理解它,我注意到当文本框有 2 方式数据绑定(bind)到属性时,该属性被调用两次。我已经通过在调用属性时向输出窗口写入一个值来验证这一点。我的代码如下:-

我的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>

我要测试的简单 View 模型类

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);
        }
    }
}

我的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();
    }
}

为什么会被调用两次。这是预期的行为吗?

最佳答案

通常,在触发 propertyChanged 事件之前,您应该检查值是否实际更改,否则您可能会进入绑定(bind)更新的无限循环。在您的情况下,文本框可能正在检查更改,从而防止出现这种循环。

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

        this._TestProperty = value;

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

关于c# - wpf 中的 2 种方式绑定(bind)两次调用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16194211/

相关文章:

c# - Find Control on RowDataBound-Event 返回 NULL/Empty

c# - 在quickgraph中获取2个节点之间的最短路径

c# - 如何将多个 ViewModel 添加到主窗口中的用户控件

c# - 如何解决 "Could not load file or assembly ' Microsoft.Practices.Prism'“错误?

c# - 从 wpf 调用 api 时 client.GetAsync 调用挂起

c# - 持续瞄准马尔默

wpf - 如何创建工具提示以显示单个控件的多个验证错误?

c# - 按下后退按钮时跳过页面,WP7

c# - 我如何从框架中获取页面实例?

c# - 在没有公共(public)构造函数的情况下实例化 .NET 类