开发者

INotifyPropertyChanged isn't working when binding dynamically using reflection

开发者 https://www.devze.com 2023-03-26 13:42 出处:网络
I\'m having an issue with the INotifyPropertyChanged event not working when binding selected item in code. Everything loads up fine but the INotifyPropertyChanged event never gets trigger when I chang

I'm having an issue with the INotifyPropertyChanged event not working when binding selected item in code. Everything loads up fine but the INotifyPropertyChanged event never gets trigger when I change my selection. Help?

public enum Foods
{
    Burger,
    Hotdog
}

public enum Drinks
{
    Pop,
    Coffee
}

// Enum collection class

public class FoodEnumerationCollection: INotifyPropertyChanged
{
    /// <summary>
    /// Declare the event
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    private Foods food;

    private Drinks drink;

    public Foods Food 
    {
        get
        {
            return this.food;
        }

        set
        {
            this.food= value;
            OnPropertyChanged("Food");
        }
    }

    public Dr开发者_StackOverflow社区inks Drink
    {
        get
        {
            return this.drink;
        }

        set
        {
            this.drink= value;
            OnPropertyChanged("Drink");
        }
    }

    #region Protected Methods
    /// <summary>
    /// Create the OnPropertyChanged method to raise the event for UI
    /// </summary>
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
    #endregion
}

In my main class I have something like this:

// instantiate food enumeration class
FoodEnumerationCollection foodEnumerationCollection = new FoodEnumerationCollection();
// Loop through all properties
foreach (PropertyInfo propertyInfo in this.foodEnumerationCollection.GetType().GetProperties())
{
            if (propertyInfo.CanWrite)
            {
                ComboBox comboBox = new ComboBox();
                comboBox.Name = propertyInfo.Name;
                comboBox.Margin = new Thickness(5, 5, 5, 5);
                comboBox.Width = 100;
                comboBox.Height = 25;

                // DataSource
                comboBox.ItemsSource = Enum.GetValues(propertyInfo.PropertyType);

                // Set binding for selected item
                object value = propertyInfo.GetValue(foodEnumerationCollection, null);
                Binding binding2 = new Binding();
                binding2.Source = propertyInfo;
                binding2.Path = new PropertyPath(propertyInfo.Name);
                binding2.Mode = BindingMode.TwoWay;
                binding2.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
                comboBox.SetBinding(ComboBox.SelectedItemProperty, binding2);

                this.FoodMenu.Items.Add(comboBox);
            }
}


The PropertyInfo class does not implement the INotifyPropertyChanged and you are setting the Source to an instance of PropertyInfo.


I'm pretty sure this line is the culprit:

binding2.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;

That will only update the binding when you explicitly tell the Binding object to update by calling UpdateSource().

You can omit that line to use the default and it should work fine... as well as heeding Erno's answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消