Here is my situation. I have multiple user controls in a canvas. This canvas is saved out to a xaml file using XamlWriter. As most of you know, bindings are not saved using this method, so when I use XamlReader and read the user control back in, the binding no longer exists.
For a simple test, I have been trying to re-bind a ComboBox ItemsSource loaded from a XAML file (which is what I’m having issues with inside the User Control). I have tried implementing INotifyPropertyChanged, however, my variable:
public event PropertyChangedEventHandler PropertyChanged
is always null when I try and set the ComboItemsProperty:
public ObservableCollection<string> ComboItemsProperty
{
get { return ComboItems; } //Field
set
{
ComboItems = value;
OnPropertyChanged("ComboItemsProperty");
}
So, my ultimate goal is to load a xaml file and then add items to a ComboBox’s ItemsSource and then have the ComboBox update with the new items.
Am I going about this the wrong way? Could someone provide me with a simple working example of this?
EDIT:
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged
if (PropertyChanged != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
I’m fairly certain it has something to do with loading the XAML and the binding no longer being set. I have tried setting the binding, but no luck.
2nd EDIT:
I think I’m 99% sure that the binding is the cause. My OnPropertyChanged works fine as long as I didn’t load the combobox from a file. I have tried setting the binding as follows:
Binding bind = new Binding();
bind.Mode = BindingMode.TwoWay;
bind.Source = this.ComboItemsProperty; //Not sure about this line.
bind.Path = new PropertyPath("ComboItemsProperty");
this.SetBiding(ComboBox.ItemsSourceProperty, bind);
Confirmed. When I bring back in a simple combobox, my attempt to bind it is not working. It must be something in the code above.
bind.Sourceneeds to point to the object containingComboItemsProperty, not the property itself.You can check the binding succeeded with: