C# 绑定控件不';更新资料来源
我对这个财产有约束力C# 绑定控件不';更新资料来源,c#,wpf,C#,Wpf,我对这个财产有约束力 私有字符串_attributeName=“”; 公共字符串属性名 { get=>\u attributeName; 设置 { _attributeName=值; OnPropertyChanged(); } } 公共事件属性更改事件处理程序属性更改; 受保护的void OnPropertyChanged([CallerMemberName]字符串名称=null) { PropertyChangedEventHandler处理程序=PropertyChanged; if(处理
私有字符串_attributeName=“”;
公共字符串属性名
{
get=>\u attributeName;
设置
{
_attributeName=值;
OnPropertyChanged();
}
}
公共事件属性更改事件处理程序属性更改;
受保护的void OnPropertyChanged([CallerMemberName]字符串名称=null)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新PropertyChangedEventArgs(名称));
}
}
使用文本框
当我在TextBox
中输入文本时,它会更改AttributeName,但当我试图通过code更改AttributeName时,TextBox
后面的文本不会更改
AttributeName=“name”//不会更改文本框。Text
您必须将数据上下文设置到您的窗口中,您可以在构造函数中执行以下操作
public MainWindow()
{
InitializeComponent();
DataContext = this; // set the code beind class to be the datacontext source for xaml
}
<TextBox Name="tbAttributeName" Text="{Binding AttributeName}"></TextBox>
并在xaml中绑定属性,如下所示
public MainWindow()
{
InitializeComponent();
DataContext = this; // set the code beind class to be the datacontext source for xaml
}
<TextBox Name="tbAttributeName" Text="{Binding AttributeName}"></TextBox>
这是什么?我猜您需要删除
ElementName
或更改到DataContext的路径。AttributeName
\u此是窗口的x:Name
的文本框所在位置。您是否将该窗口的DataContext设置为AttributeName
所在位置?否。AttributeName位于窗口
的代码隐藏。您是否在窗口
类中实现了inotifPropertyChanged
接口?请确保您的窗口将inotifPropertyChanged
接口实现为更新的代码是的,我忘了实现该接口