gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:07:00 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com