设置值时 WPF 文本框绑定不更新
WPF Textbox Binding Is Not Updating When Setting Value
我有一个小程序来测试文本框数据绑定,当我更改文本框本身的值时它可以工作,但是当我尝试从代码隐藏更改值时,变量已更新但文本框是没有更新。我环顾四周但未能找到解决方案,因为我所看到的大多数都是针对更新变量的文本框,这与我需要的相反。这是代码:
XAML:
<Grid>
<TextBox Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="127,37,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox Text="{Binding LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="127,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBlock HorizontalAlignment="Left" Margin="28,40,0,0" TextWrapping="Wrap" Text="First Name" VerticalAlignment="Top"/>
<TextBlock HorizontalAlignment="Left" Margin="28,91,0,0" TextWrapping="Wrap" Text="Last Name" VerticalAlignment="Top"/>
<Button x:Name="Name" Content="Name!" HorizontalAlignment="Left" Margin="145,212,0,0" VerticalAlignment="Top" Width="75" Click="Name_Click"/>
</Grid>
还有我的代码:
namespace WpfApplication1
{
public class UIControls : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _firstName;
private string _lastName;
protected void Notify(string propertyName)
{
if(this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public string FirstName
{
get { return _firstName; }
set
{
if (value != _firstName)
{
_firstName = value;
Notify("FirstName");
}
}
}
public string LastName
{
get { return _lastName; }
set
{
if (value != _lastName)
{
_lastName = value;
Notify("LastName");
}
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
UIControls viewModel = new UIControls();
this.DataContext = viewModel;
}
private void Name_Click(object sender, RoutedEventArgs e)
{
UIControls ui = new UIControls();
ui.FirstName = "Mike";
ui.LastName = "Smith";
}
}
}
好吧,您在每次单击按钮时都创建了新的 UIControl。新的 UIControls 未绑定到文本框。尝试创建绑定到 MainWindow 的私有文件,如下所示:
public partial class MainWindow : Window
{
private UIControls viewModel;
public MainWindow()
{
InitializeComponent();
viewModel = new UIControls();
this.DataContext = viewModel;
}
private void Name_Click(object sender, RoutedEventArgs e)
{
viewModel.FirstName = "Mike";
viewModel.LastName = "Smith";
}
我有一个小程序来测试文本框数据绑定,当我更改文本框本身的值时它可以工作,但是当我尝试从代码隐藏更改值时,变量已更新但文本框是没有更新。我环顾四周但未能找到解决方案,因为我所看到的大多数都是针对更新变量的文本框,这与我需要的相反。这是代码:
XAML:
<Grid>
<TextBox Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="127,37,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox Text="{Binding LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="127,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBlock HorizontalAlignment="Left" Margin="28,40,0,0" TextWrapping="Wrap" Text="First Name" VerticalAlignment="Top"/>
<TextBlock HorizontalAlignment="Left" Margin="28,91,0,0" TextWrapping="Wrap" Text="Last Name" VerticalAlignment="Top"/>
<Button x:Name="Name" Content="Name!" HorizontalAlignment="Left" Margin="145,212,0,0" VerticalAlignment="Top" Width="75" Click="Name_Click"/>
</Grid>
还有我的代码:
namespace WpfApplication1
{
public class UIControls : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _firstName;
private string _lastName;
protected void Notify(string propertyName)
{
if(this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public string FirstName
{
get { return _firstName; }
set
{
if (value != _firstName)
{
_firstName = value;
Notify("FirstName");
}
}
}
public string LastName
{
get { return _lastName; }
set
{
if (value != _lastName)
{
_lastName = value;
Notify("LastName");
}
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
UIControls viewModel = new UIControls();
this.DataContext = viewModel;
}
private void Name_Click(object sender, RoutedEventArgs e)
{
UIControls ui = new UIControls();
ui.FirstName = "Mike";
ui.LastName = "Smith";
}
}
}
好吧,您在每次单击按钮时都创建了新的 UIControl。新的 UIControls 未绑定到文本框。尝试创建绑定到 MainWindow 的私有文件,如下所示:
public partial class MainWindow : Window
{
private UIControls viewModel;
public MainWindow()
{
InitializeComponent();
viewModel = new UIControls();
this.DataContext = viewModel;
}
private void Name_Click(object sender, RoutedEventArgs e)
{
viewModel.FirstName = "Mike";
viewModel.LastName = "Smith";
}