gpt4 book ai didi

c# - 通过 INotifyPropertyChanged 写入 TextBox 无效

转载 作者:太空宇宙 更新时间:2023-11-03 23:32:35 24 4
gpt4 key购买 nike

我在调用 INotifyPropertyChanged 事件时遇到问题。我创建了一个简单的概念 WPF 程序,它试图通过一个按钮写入文本框,该按钮调用一个更新属性 (ViewModel1StringProperty) 的函数。该属性绑定(bind)到文本框,并且该属性调用 INotifyPropertyChanged 事件。我已将此文本属性添加到文本框:

Text="{Binding ViewModel1StringProperty, UpdateSourceTrigger=PropertyChanged}"

但是,单击按钮时文本框仍未更新。需要注意的一件事是按钮调用 ICommand,然后最终通过函数调用 (TestFunction1) 更新属性。我相信此命令已正确绑定(bind)到按钮,但我不确定。

任何对此问题的见解都将不胜感激。

View 模型代码:

    namespace WpfApplicationOnPropertyChanged.ViewModel
{
class ViewModel1 : INotifyPropertyChanged
{
#region Fields
string _viewModel1StringProperty;
public event PropertyChangedEventHandler PropertyChanged;
#endregion

protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

#region Properties
public string ViewModel1StringProperty
{
get
{
return _viewModel1StringProperty;
}

set
{
_viewModel1StringProperty = value;
NotifyPropertyChanged("ViewModel1StringProperty");
}
}
#endregion

#region Commands
public ICommand ViewModelICommandField1
{
get
{
return new ViewModelICommand(TestFunction1);
}
}
#endregion

#region Functions

private void TestFunction1()
{
ViewModel1StringProperty = "Test1\n";
}
#endregion
}
}

查看代码:

    namespace WpfApplicationOnPropertyChanged.View
{
/// <summary>
/// Interaction logic for View1.xaml
/// </summary>
public partial class View1 : UserControl
{
#region Fields
ViewModel1 _viewModel1 = new ViewModel1();
#endregion

public View1()
{
InitializeComponent();
this.DataContext = _viewModel1;
}
}
}

XAML 代码:

    <UserControl x:Class="WpfApplicationOnPropertyChanged.View.View1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml /presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplicationOnPropertyChanged.View"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox Name="textBox1" HorizontalAlignment="Left" Height="89" Margin="58,62,0,0" Text="{Binding ViewModel1StringProperty, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap" VerticalAlignment="Top" Width="181"/>
<Button Content="Button1" Command="{Binding ViewModel1ICommandField1}" HorizontalAlignment="Left" Margin="106,205,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</UserControl>

I命令代码:

    namespace WpfApplicationOnPropertyChanged.ViewModel
{
class ViewModelICommand : ICommand
{
private Action _action;

public ViewModelICommand(Action action)
{
_action = action;
}

public void Execute(object parameter)
{
_action();
}

public bool CanExecute(object parameter)
{
return true;
}

public event EventHandler CanExecuteChanged
{
add { }
remove { }
}
}
}

最佳答案

在 XAML 中,您已将按钮的命令绑定(bind)到 ViewModel1ICommandField1,但在 View 模型中,它的名称是 ViewModelICommandField1

请重命名,以便它们匹配,您将得到正确的行为。

关于c# - 通过 INotifyPropertyChanged 写入 TextBox 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31601749/

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