c# - 简单的 WPF 数据绑定(bind)导致 StackOverFlow 异常

标签 c# wpf data-binding

我的 WPF 项目中有以下 XAML:

<Window x:Class="DataBindToLocalVars.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">
<Grid>
    <TextBox x:Name="txt2" HorizontalAlignment="Left" Height="23" Margin="150,94,0,0" TextWrapping="Wrap" Text="{Binding Path=value1}" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="txt1" HorizontalAlignment="Left" Height="23" Margin="150,140,0,0" TextWrapping="Wrap" Text="{Binding Path=value2}" VerticalAlignment="Top" Width="120"/>
    <Label Content="Value 2" HorizontalAlignment="Left" Margin="107,91,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.577,-0.602"/>
    <Label Content="Value1" HorizontalAlignment="Left" Margin="107,145,0,0" VerticalAlignment="Top"/>

</Grid>

以及我的代码隐藏文件中的以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace DataBindToLocalVars
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public string value1
    {
        get { return value1; }
        set
        {
            value1 = value;
            this.NotifyPropertyChange("value1");
        }
    }
    public string value2 {
        get { return value2; }
        set {
        value2 = value;
        this.NotifyPropertyChange("value2");
    } }
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;

        value1 = "20";
        value2 = "40"; 
    }

    public void NotifyPropertyChange(string propName)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}
}

我正在实现 INotifyPropertyChange 接口(interface),以便在属性 value1 和 value2 更改时收到通知。但是运行这段代码给了我一个堆栈溢出异常。这是为什么?

最佳答案

这就是你的原因:

public string value1
{
    get { return value1; }
    set
    {
        value1 = value;
        this.NotifyPropertyChange("value1");
    }
}

您的属性 getter 和 setter 是完全递归的(它们会导致对自身的调用)。

惯用的 C# 使用 PascalCasing 作为属性名称,那么怎么样:

private string value1;

public string Value1
{
    get { return value1; }
    set
    {
        value1 = value;
        this.NotifyPropertyChange("Value1");
    }
}

这里的 getter 和 setter 使用私有(private) value1 字段。

您的 INotifyPropertyChanged 实现也存在问题。当事件没有订阅者时,处理程序将为 null。您需要防止这种情况,否则您将收到 NullReferenceException:

public void NotifyPropertyChange(string propName)
{
    var handler = PropertyChanged;
    if (handler != null) handler(this, new PropertyChangedEventArgs(propName));
}

关于c# - 简单的 WPF 数据绑定(bind)导致 StackOverFlow 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30438619/

相关文章:

c# - 在不阻止其他进程同时读取文件的情况下读取文件

c# - 带有货币 double 值的字符串格式显示不正确。

c# - 带有帐户的新 Web 应用程序

wpf - 如何在设计模式的上下文菜单中插入/删除 WPF 网格行/列?

c# - 如何将二维数组 bool[][] 绑定(bind)到 WPF DataGrid(单向)?

c# - 如何使用linq分配两个列表,并使用int创建一个新对象的列表?

c# - 串联列表中的项目以在 WPF 组合框 Itemssource 中使用

javascript - 将 JSON 对象绑定(bind)到 HTML 元素

c# - 为什么datagridview不刷新?

wpf - 鼠标绑定(bind)鼠标滚轮以放大 WPF 和 MVVM