数据绑定不更新
Data binding not updating
我有一个存储在客户列表中的实体集合,一个绑定到该列表的组合框和一个在组合框中获取选定客户的对象 "SelectedCustomer",但问题是我有一个文本框绑定到该对象,每当我在该对象中存储集合中的新元素时它都不会更新
这是我的代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
using (var context = new Entities())
{
Customers = context.PERFCONTENEUR.ToList();
CustomersCombo.SelectedItem = Customers[0];
}
DataContext = this;
}
public List<PERFCONTENEUR> Customers { get; set; }
public PERFCONTENEUR SelectedCustomer { get; set; }
private void move(object sender, MouseButtonEventArgs e)
{
DragMove();
}
private void main1_Loaded(object sender, RoutedEventArgs e)
{
}
private void CustomersCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedCustomer = CustomersCombo.SelectedItem as PERFCONTENEUR;
}
private void CustomersCombo_Loaded(object sender, RoutedEventArgs e)
{
}
}
这是我的 XAML
<TextBox Text="{Binding SelectedCustomer.ID}" HorizontalAlignment="Left" Height="23" Margin="548,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<ComboBox x:Name="CustomersCombo"
ItemsSource="{Binding Customers}"
DisplayMemberPath="CLIENT"
SelectedValuePath="ID"
HorizontalAlignment="Left" Margin="421,54,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="CustomersCombo_SelectionChanged" />
这是我在 Perfconteneur Class 中实现的接口 INotifyPropertyChanged,它是 SelectedCustomer 对象的类型
public部分classPERFCONTENEUR:INotifyPropertyChanged
{ public event PropertyChangedEventHandler PropertyChanged;
private string _CLIENT;
public decimal ID { get; set; }
public Nullable<decimal> TAILLE { get; set; }
public string CLIENT
{
get { return _CLIENT; }
set
{
if (value != _CLIENT)
{
_CLIENT = value;
NotifyPropertyChanged("CLIENT");
}
}
}
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public string D_ARRIVEE { get; set; }
public virtual PERFREPARATION PERFREPARATION { get; set; }
}
您需要让您的 SelectedCostumer 属性 通知 UI 稍后更改;将 INPC 实现添加到您的 MainWindow
private PERFCONTENEUR _selectedCustomer;
public PERFCONTENEUR SelectedCustomer
{
get { return _selectedCustomer; }
set
{
if (value != _selectedCustomer)
{
_selectedCustomer = value;
NotifyPropertyChanged("SelectedCustomer");
}
}
}
我不想对违反约定的问题发表更多评论,请尝试采用通用的编码约定,因为其他程序员更容易理解您的代码
我有一个存储在客户列表中的实体集合,一个绑定到该列表的组合框和一个在组合框中获取选定客户的对象 "SelectedCustomer",但问题是我有一个文本框绑定到该对象,每当我在该对象中存储集合中的新元素时它都不会更新
这是我的代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
using (var context = new Entities())
{
Customers = context.PERFCONTENEUR.ToList();
CustomersCombo.SelectedItem = Customers[0];
}
DataContext = this;
}
public List<PERFCONTENEUR> Customers { get; set; }
public PERFCONTENEUR SelectedCustomer { get; set; }
private void move(object sender, MouseButtonEventArgs e)
{
DragMove();
}
private void main1_Loaded(object sender, RoutedEventArgs e)
{
}
private void CustomersCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedCustomer = CustomersCombo.SelectedItem as PERFCONTENEUR;
}
private void CustomersCombo_Loaded(object sender, RoutedEventArgs e)
{
}
}
这是我的 XAML
<TextBox Text="{Binding SelectedCustomer.ID}" HorizontalAlignment="Left" Height="23" Margin="548,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<ComboBox x:Name="CustomersCombo"
ItemsSource="{Binding Customers}"
DisplayMemberPath="CLIENT"
SelectedValuePath="ID"
HorizontalAlignment="Left" Margin="421,54,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="CustomersCombo_SelectionChanged" />
这是我在 Perfconteneur Class 中实现的接口 INotifyPropertyChanged,它是 SelectedCustomer 对象的类型
public部分classPERFCONTENEUR:INotifyPropertyChanged
{ public event PropertyChangedEventHandler PropertyChanged;
private string _CLIENT;
public decimal ID { get; set; }
public Nullable<decimal> TAILLE { get; set; }
public string CLIENT
{
get { return _CLIENT; }
set
{
if (value != _CLIENT)
{
_CLIENT = value;
NotifyPropertyChanged("CLIENT");
}
}
}
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public string D_ARRIVEE { get; set; }
public virtual PERFREPARATION PERFREPARATION { get; set; }
}
您需要让您的 SelectedCostumer 属性 通知 UI 稍后更改;将 INPC 实现添加到您的 MainWindow
private PERFCONTENEUR _selectedCustomer;
public PERFCONTENEUR SelectedCustomer
{
get { return _selectedCustomer; }
set
{
if (value != _selectedCustomer)
{
_selectedCustomer = value;
NotifyPropertyChanged("SelectedCustomer");
}
}
}
我不想对违反约定的问题发表更多评论,请尝试采用通用的编码约定,因为其他程序员更容易理解您的代码