1

With the following code I expect the combobox to revert back to the initial Selected_Item, however it does not, the ComboBox on the window always shows any new item I select (and gets out of sync with the model), i also tried to call always the OnPropertyChanged("Selected_Item"); no matter whether the value is different, and still the combobox shows the newly selected item and gets out of sync. Probably i am doing it the wrong way, however, what would be the correct way?

namespace WpfApplication8
{
    public class Context : INotifyPropertyChanged
    {
        #region Privates

        bool c_Disabled = false;

        #endregion

        #region Ctors

        public Context()
        {
            Items = new List<MyItem>();

            Items.Add(new MyItem("Item 1"));
            Items.Add(new MyItem("Item 2"));
            Items.Add(new MyItem("Item 3"));

            Selected_Item = Items[0];

            c_Disabled = true;
        }

        #endregion

        #region Properties

        public List<MyItem> Items
        {
            get;
            private set;
        }

        private MyItem c_Selected_Item;
        public MyItem Selected_Item
        {
            get { return c_Selected_Item; }
            set
            {
                if (c_Selected_Item != value)
                {
                    if (!c_Disabled)
                    {
                        c_Selected_Item = value;

                        OnPropertyChanged("Selected_Item");
                    }

                }
            }
        }


        #endregion

        #region INotifyPropertyChanged implementation

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string f_Prop_Name)
        {
            PropertyChangedEventHandler l_Handler = PropertyChanged;
            if(null != l_Handler)
            {
                l_Handler(this, new PropertyChangedEventArgs(f_Prop_Name));
            }
        }

        #endregion
    }

    public class MyItem
    {
        public MyItem(string f_Name)
        {
            Name = f_Name;
        }

        public string Name {get;set;}
    }
}

and the following window:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox
            HorizontalAlignment="Center"
            VerticalAlignment="Top"
            ItemsSource="{Binding Items}"
            SelectedItem="{Binding Selected_Item}">

            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>

        </ComboBox>
    </Grid>
</Window>
user1464603
  • 437
  • 4
  • 8
  • c_Disabled is always true. – Jammer Jun 22 '18 at 08:20
  • Your problem is not clear. It is obvious that your if condition is always true. Maybe if you state your requirement clearly, people can suggest you where you have gone wrong. – Swamy Jun 22 '18 at 08:22
  • 2
    Your `Selected_Item` setter can't prevent that the ComboBox's SelectedItem is changing, even when it does not set its backing field. As long as the Binding isn't reevaluated, nothing happens. – Clemens Jun 22 '18 at 08:23
  • @Jammer No, it is not always true – Sir Rufo Jun 22 '18 at 08:26
  • I think what you need is `DependencyProperty` and `Coerce Value Callbacks` to solve your problem. Please do check this link:https://stackoverflow.com/questions/30379687/what-is-the-need-for-coercing-a-dependency-property – Swamy Jun 22 '18 at 08:29
  • I want to NOT update the selection from the outside world (i.e. from the view, by disabling the assignment), however even if Selected_Item on the model does not change, the View is totally ignoring this and still sticking with whatever selection the user makes from the UI. – user1464603 Jun 22 '18 at 08:31
  • 2
    "the View is totally ignoring this" - sure, because your assumption about how Bindings work is wrong. – Clemens Jun 22 '18 at 08:32
  • @Clemens, thank you i figured that, now a piece of document from which I should have inferred that my "about how Bindings work assumption is wrong"? – user1464603 Jun 22 '18 at 08:35
  • You are assuming that after the Binding has set the source property value, it will automatically get the actual value back from its source property. What makes you think so? – Clemens Jun 22 '18 at 08:36
  • 1
    @Clemens, no i am assuming that it does not work because I said [quote]i also tried to call always the OnPropertyChanged("Selected_Item")[/quote]. Meaning that even if i retrigger a property update when the set is ignored, it gets ignored by the UI. – user1464603 Jun 22 '18 at 14:07

1 Answers1

-1

Remove c_Disabled = true; from the Context method;

horotab
  • 675
  • 3
  • 20