C# 如何将代码隐藏中的自定义对象绑定到XAML中的网格?
在下面的示例中,Message属性正确绑定,但Customer对象的FirstName、LastName和Age属性为空。为什么呢 XAML:C# 如何将代码隐藏中的自定义对象绑定到XAML中的网格?,c#,wpf,data-binding,xaml,C#,Wpf,Data Binding,Xaml,在下面的示例中,Message属性正确绑定,但Customer对象的FirstName、LastName和Age属性为空。为什么呢 XAML: <Window x:Class="TestBinding9922.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<Window x:Class="TestBinding9922.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<TextBlock Text="{Binding Message}"/>
<Grid DataContext="{Binding Customer}" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0" Content="First Name:" />
<Label Grid.Column="0" Grid.Row="1" Content="Last Name:" />
<Label Grid.Column="0" Grid.Row="2" Content="Age:" />
<TextBox Grid.Column="1" Grid.Row="0" Text="{Binding FirstName}" />
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding LastName}" />
<TextBox Grid.Column="1" Grid.Row="2" Text="{Binding Age}" />
</Grid>
</StackPanel>
</Window>
using System.Windows;
using System.ComponentModel;
namespace TestBinding9922
{
public partial class Window1 : Window, INotifyPropertyChanged
{
public string Message { get; set; }
public Customer Customer { get; set; }
public Window1()
{
InitializeComponent();
DataContext = this;
Message = "this works";
Customer customer = new Customer() { FirstName = "Jim", LastName = "Smith", Age = 45 };
}
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
}
增编:
即使使用INotifyPropertyChanged,文本框仍然为空:
using System.Windows;
using System.ComponentModel;
namespace TestBinding9922
{
public partial class Window1 : Window, INotifyPropertyChanged
{
#region ViewModelProperty: Message
private string _message;
public string Message
{
get
{
return _message;
}
set
{
_message = value;
OnPropertyChanged("Message");
}
}
#endregion
#region ViewModelProperty: Customer
private Customer _customer;
public Customer Customer
{
get
{
return _customer;
}
set
{
_customer = value;
OnPropertyChanged("Customer");
}
}
#endregion
public Window1()
{
InitializeComponent();
DataContext = this;
Message = "this works";
Customer customer = new Customer() { FirstName = "Jim", LastName = "Smith", Age = 45 };
}
#region INotifiedProperty Block
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
}
您的Customer类需要实现INotifyPropertyChanged,随后正在更改的属性需要在更改时通知 你可以在其他地方找到这样的例子 此外,您不是在实例化客户属性,而是在创建一个局部变量;应该是:
Customer = new Customer()
{ FirstName = "Jim", LastName = "Smith", Age = 45 };
看起来您正在将新的Customer实例分配给一个局部变量“Customer”,而不是窗口的属性。您可以删除XAML中网格的DataContext设置,然后将文本框绑定到Customer.FirstName等
或者,您可以像以前一样删除XAML中网格的DataContext设置,但给网格一个名称,然后在代码中设置其DataContext,就像您为窗口所做的那样。我无法相信您链接的博客条目的作者会认为他的解决方案简单而优雅。如果WPF真的简化了开发,我会是一个粉丝,但在使用了相当多之后,我觉得它只是将痛苦推到了新的和未记录的领域。谢谢,我监督我定义了一个局部变量,它现在可以工作了,即使没有INotifyPropertyChanged@Peter是的,数据绑定现在不是件好事。我甚至不确定你是否一直都需要它。我很确定,如果要进行双向绑定,您需要实现INotifyPropertyChanged方案。