How can I catch an event of an Object inside a ComboBox? - vb.net

So basically I have a ComboBox on a form, which I populated by adding custom object named "Category" and by setting the DisplayMember to the property "Name" of my object.
On another form that can be opened at the same time, I can edit the name of theses "Category" objects. I raise an Event, "NameChanged" but how can I catch it on the form which contains the ComboBox ?
Even if the property "Name" of the object "Category" change, the display on the ComboBox doesn't autoupdate. So I need to catch the event, but I don't know how to do it.
Thanks to anyone who can help me.

If you make your Category class implement INotifyPropertyChanged, you can handle events when a property changes.
To do so, you have to change your property from a simple property:
// will NOT raise event
public string Name { get; set; }
to something more like:
// will raise event
public string Name
{
get { return _Name; }
set
{
if (_Name != value)
{
_Name = value;
OnPropertyChanged("Name");
}
}
}
private string _Name;
and then implement INotifyPropertyChanged in your class as well:
public event EventHandler<PropertyChangedEventArgs> PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
protected virtual void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
Now, when adding a Category object to your ComboBox, subscribe to the PropertyChanged event which will be raised every time the Name property changes.
An Even Better Way
Consider using the Binding class to populate your ComboBox. Binding automagically uses INotifyPropertyChanged to update the display when a property value changes.

Related

Why will a child property not update when databinding in XAML

Can someone explain to me why the first property below (Name) updates fine from the UI, but the second one (End) does not? Both properties display correctly, so it IS bound. It just won't update the child property.
Period.Period (not my choice in the naming) is defined as a datetimeoffset.
<custom:FieldControl TargetObject="{Binding Path=Period}" TargetProperty="Name" IsReadOnly="False" />
<custom:FieldControl TargetObject="{Binding Path=Period.Period}" TargetProperty="End" IsReadOnly="False" />
I'm very new to XAML, so, if I haven't included enough detail, let me know and I'll edit the question.
You probably need to implement a INotifyPropertyChanged.
Here is an example with your two properties:
public class PeriodSample : INotifyPropertyChanged
{
private string name;
private DateTimeOffset period;
public event PropertyChangedEventHandler PropertyChanged;
public string Name
{
get
{
return name;
}
set
{
name = value;
OnPropertyChanged(nameof(Name));
}
}
public DateTimeOffset Period
{
get
{
return period;
}
set
{
period = value;
OnPropertyChanged(nameof(Period));
}
}
public PeriodSample()
{
}
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
This concept allow to notify the Control when the binded properties changed.
If that doesn't solve the problem, could you provide a more complete example please?

How to add fields that dinamically change values in binded variables

In order to explain my problem let's say I have in my ViewModel an ObservableCollection which has several elements of my type Item defined in my Models. These items as in any other shop have a name and a price and are all listed in one of my views.
My question is if there is any way to create variables to alter, for example, the price of each item dynamically. What I would like to do is: for each element in my listView, have one or more entries to allow me to customize his characteristics. Let's say that the user enters the value 2 in one of this entries, the price of the item corresponding to that line of the listView should be 2x higher but the others must remain the same.
To make it a practical example, take into consideration the following image. Let's say the first line is the name of the product and the second line is the price, I would like to have, in each row, at least one entry to allow me to customize the value of the price. Is it possible?
Article List
You can create a new property to bind to Label.Text, and update it when the property bound to the Entry.Text property changes.
Yes,
you can add the Entry to your ViewCell, and bind the Entry to the same property you bind to the Label. When you change the value in the Entry, the value in the Label should change.
Possibly the simplest way to achieve this is to wrap each Item in an ItemViewModel.
The ItemViewModel has a public Property Multiplier where - in the Setter - the price of the containing Item will be updated.
For the price change to be reflected in the view - it must implement INotifyPropertyChanged and of course raise the event when the 'Price' is set.
Alternatively: copy the Price from the Item to the ItemViewModel in the constructor to an additional Price property on the ItemViewModel
With the sample code you'd need a IValueConverter to convert the Entry Text from string to double (or use an apropriate control that allows binding to double)
public class ItemsViewModel
{
public ObservableCollection<ItemViewModel> Items { get; set; }
public ItemsViewModel()
{
this.Items = new ObservableCollection<ItemViewModel>(getItemsFromSomewhere().Select(item => new ItemViewModel(item)));
}
}
public class ItemViewModel : INotifyPropertyChanged
{
private readonly Item item;
private double price;
private double multiplicator;
public ItemViewModel(Item item)
{
this.item = item;
this.price = item.Price;
}
public double Multiplicator {
get { return this.multiplicator; }
set {
this.multiplicator = value;
this.Price = this.item.Price * value;
}
}
public double Price {
get { return this.price; }
set {
this.price = value;
this.OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Item
{
public double Price { get; set; }
}

My checkbox is not binding with the member

Say we have a grid view which is binding with the data source MyInformation. One of column is a check box. I want to bind something with it.
ItemsSource="{Binding MyInformation}"
In the ViewModel.
public ObservableCollection<Container> MyInformation
{
get
{
if (this.myInformation == null)
{
this.myInformation = new ObservableCollection<Container>();
}
return this.myInformation;
}
set
{
if (this.myInformation != value)
{
this.myInformation = value;
this.OnPropertyChanged("MyInformation");
}
}
}
The class Container has a member "GoodValue".
public class Container
{
public bool GoodValue {get;set;}
//
}
I have the checkbox bind with the member.
<DataTemplate>
<CheckBox HorizontalAlignment="Center" IsChecked="{Binding GoodValue, Converter={StaticResource ShortToBooleanConverter}}" Click="CheckBox_Checked"></CheckBox>
</DataTemplate>
I don't have the property GoodValue created in ViewModel as I think GoodValue is a member of Container. The ObservableCollection includes it automatically.
The problem is each time I read the data from the database. The checkbox is unchecked. So I doubt my code. Thanks for hint.
You can do two things:
Check if there are some binding errors
Implement INotifyPropertyChanged interface into your class Container.
public class Container:INotifyPropertyChanged
{
private bool _goodValue;
public string GoodValue
{
get
{
return _goodValue;
}
set
{
_goodValue = value;
OnPropertyChanged("GoodValue");
}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public event PropertyChangedEventHandler PropertyChanged;
}
The ObservableCollection is usefull if you want to notify to your view when a new item is inserted or deleted from the collection, but if the object contained inside it doesn't implement InotifyPropertyChanged, the changes to properties of that object won't affect any change to your view.

INotifyPropertyChanged best practices

When I have a class that implements INotifyPropertyChanged, is it ok to expose the implementation as a public method?
For instance, if I have a property called "Sum" on a class, and I want a button click in the UI to update the sum, what is the best way to do this?
Below is some pseudo-code to illustrate what I mean
classinstance.NotifyPropertyChanged("Sum");
...
public Sum {
get { return x + y + z; }
}
In .Net the preferred practice for methods that raise events is for the method to be declared as protected so that it can only be called by derived classes (This is because only the class that declares the event can raise it. In order to raise the event from a derived class a method is required to raise the event).
For example...
protected void OnPropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
This method is then called by the class (or derived classes) in a property setter to indicate that a property has changed, like so...
public object MyProperty
{
get { return _myProperty; }
set
{
_myProperty = value;
OnPropertyChanged("MyProperty");
}
}
Other objects can then subscribe to this event and will be notified every time the MyProperty property is changed.
Now, to answer your question as to whether the OnPropertyChanged method can be public. The answer is yes but you should be asking yourself why this would be the case.
Why would another class know when a property has changed so that it can call the method? if it already 'knows' when the property has changed then you shouldn't need to subscribe to the property changed event in the first place! Only the class itself should 'know' when one of its own properties has changed.
In your example You are notifying that the property 'sum' has been changed. but it hasn't. In fact, your code doesn't even allow that property to be changed outside of its own class.
I suspect that maybe you want some way of notifying that the sum property needs to be re-evaluated because a dependent property has been changed. If this is the case then you need to raise a property changed event when that dependent property changes.
Imagine that changes to the 'MyProperty' property shown earlier also means that 'Sum' has changed then that would be handled like this:
// This property is used by the 'sum' property so if this changes
// clients need to know that 'sum' has also changed.
public object MyProperty
{
get { return _myProperty; }
set
{
_myProperty = value;
OnPropertyChanged("MyProperty");
OnPropertyChanged("Sum");
}
}
as for much more pretty to implement base :
public abstract class NotificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged<T>(Expression<Func<T>> me)
=> RaisePropertyChanged((me.Body as MemberExpression)?.Member.Name);
protected virtual void RaisePropertyChanged(string propertyName)
=> PropertyChanged?.Invoke(this, propertyName);
}
It's also worthy to lookup for https://msdn.microsoft.com/en-us/magazine/mt736453.aspx

Silverlight MVVM, stop SelectionChanged triggering in response to ItemsSource reset

I have two ComboBoxes, A & B, each bound to an Observable Collection. Each has a SelectionChanged trigger is attached which is intended to catch when the user changes a selection. The trigger passes the selection to a Command.
The collections implement INotifyPropertyChanged in that, in the Setter of each, an NotifyPropertyChanged event is fired. This is needed (in the MVVM approach) to notify the UI (the View) that the ComboBox's contents have changed.
The two ComboBoxes are interdependent - changing the selection in A causes B to be repopulated with new items.
Now, the problem is that B's SelectionChanged trigger fires in response to its collection being repopulated (as well as the user changing a selection). Due to the complexity of the code in the Command this is a huge waste of resources.
I could in theory stop this by not raising the NotifyPropertyChanged event when B's collection is set (because, looking at the Call Stack, this is what seems to cause the SelectionChanged trigger to fire), however the MVVM approach depends on this to keep the UI refreshed.
Any suggestions?
Why does ComboB need a SelectionChanged event? You can just bind the selected item directly into a property on the VM.
The way i have tackled this previously was to bind ComboA's selected item into the VM. In the setter for that property, i recalculate the available items for ComboB and assign them to another property on the VM, and ComboB's ItemsSource is bound to this property. Of course that property will notify (using INotifyPropertyChanged), but nothing else needed to be done, my ComboB did not have a SelectionChanged event. By using this method i didn't need a SelectionChanged on ComboA either, which keeps the view's code behind nice and sparse - everything is handled in the VM and regular databinding takes care of the rest.
Edit:
Here is an example of adjusting the required lists from within the property setters:
public class MyViewModel : INotifyPropertyChanged
{
//ItemsSource of ComboA is bound to this list
public List<SomeObject> ComboAList
{
get { return _comboAList; }
set { _comboAList = value; }
}
//ItemsSource of ComboB is bound to this list
public List<SomeObject> ComboBList
{
get { return _comboBList; }
set
{
_comboBList = value;
OnPropertyChanged("ComboBList");
}
}
//ItemsSource of the dataGrid is bound to this list
public List<SomeObject> DataGridList
{
get { return _datagridList; }
set
{
_datagridList = value;
OnPropertyChanged("DataGridList");
}
}
//SelectedItem of ComboA is bound to this property
public SomeObject FirstSelectedItem
{
get { return _firstSelectedItem; }
set
{
_firstSelectedItem = value;
RefreshListForComboB();
}
}
//SelectedItem of ComboB is bound to this property
public SomeObject SecondSelectedItem
{
get { return _secondSelectedItem; }
set
{
_secondSelectedItem = value;
RefreshListForDataGrid();
}
}
private void RefreshListForComboB()
{
//do whatever is necessary to filter or create a list for comboB
ComboBList = doSomethingThatReturnsAListForComboB();
}
private void RefreshListForDataGrid()
{
//do whatever is necessary to filter or create the list for the DataGrid
DataGridList = doSomethingThatReturnsAListForDataGrid();
}
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
private List<SomeObject> _comboAList, _comboBList, _datagridList;
private SomeObject _firstSelectedItem, _secondSelectedItem;
}
And here is a slightly different way to do it, using a PropertyChange event handler on the VM, this simply changes where the list updating happens. This is arguably a better way of doing it than the first sample as it means the property setters don't have side effects:
public class MyViewModel : INotifyPropertyChanged
{
public MyViewModel()
{
this.PropertyChanged += new PropertyChangedEventHandler(MyViewModel_PropertyChanged);
}
private void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "FirstSelectedItem":
RefreshListForComboB();
break;
case "SecondSelectedItem":
RefreshListForDataGrid();
break;
}
}
//ItemsSource of ComboA is bound to this list
public List<SomeObject> ComboAList
{
get { return _comboAList; }
set { _comboAList = value; }
}
//ItemsSource of ComboB is bound to this list
public List<SomeObject> ComboBList
{
get { return _comboBList; }
set
{
_comboBList = value;
OnPropertyChanged("ComboBList");
}
}
//ItemsSource of the dataGrid is bound to this list
public List<SomeObject> DataGridList
{
get { return _datagridList; }
set
{
_datagridList = value;
OnPropertyChanged("DataGridList");
}
}
//SelectedItem of ComboA is bound to this property
public SomeObject FirstSelectedItem
{
get { return _firstSelectedItem; }
set
{
_firstSelectedItem = value;
OnPropertyChanged("FirstSelectedItem");
}
}
//SelectedItem of ComboB is bound to this property
public SomeObject SecondSelectedItem
{
get { return _secondSelectedItem; }
set
{
_secondSelectedItem = value;
OnPropertyChanged("SecondSelectedItem");
}
}
private void RefreshListForComboB()
{
//do whatever is necessary to filter or create a list for comboB
ComboBList = doSomethingThatReturnsAListForComboB();
}
private void RefreshListForDataGrid()
{
//do whatever is necessary to filter or create the list for the DataGrid
DataGridList = doSomethingThatReturnsAListForDataGrid();
}
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
private List<SomeObject> _comboAList, _comboBList, _datagridList;
private SomeObject _firstSelectedItem, _secondSelectedItem;
}