Reputation: 3
I have a collection of entities stored in a List of Customers, a combobox bound to that list and an object "SelectedCustomer" that gets the selected customer in the combobox , but here s the problem i have a textbox bound to that object ,which not updating whenever i store in that object a new element from the collection
Here s my code
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)
{
}
}
Here s my 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" />
And here s my implementation of the Interface INotifyPropertyChanged in the Perfconteneur Class which is the type of the SelectedCustomer object
public partial class PERFCONTENEUR: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; }
}
Upvotes: 0
Views: 109
Reputation: 2207
You need to make you SelectedCostumer property notify the UI when this later change; add the INPC implemetation to your MainWindow
private PERFCONTENEUR _selectedCustomer;
public PERFCONTENEUR SelectedCustomer
{
get { return _selectedCustomer; }
set
{
if (value != _selectedCustomer)
{
_selectedCustomer = value;
NotifyPropertyChanged("SelectedCustomer");
}
}
}
I don't want to comment more about the conventions violation, try to adopt the common coding conventions because it will be much easier for other programmers to understand your code
Upvotes: 1