Question

I have currently the problem that my UI doesnt update as I like to do so, hope you can help me out.

I have a simulated "2 class inheritance" as recommended in this page http://www.codeproject.com/Articles/10072/Simulated-Multiple-Inheritance-Pattern-for-C

My real life app looks like the following:

public class Item : INotifyPropertyChanged
{
   private bool _isVisible;
   public bool IsVisible
   {
      get
      {
         return _isVisible;
      }
      set
      {
         if (_isVisible == value)
            return;

         _isVisible = value;
         OnPropertyChanged("IsVisible");
      }
   }

   //NotifyPropertyChanged Implementation removed so the focus stays on problem...
}

public class ObjectItem : INotifyPropertyChanged
{
   private bool _isExpanded;
   public bool IsExpanded
   {
      get
      {
         return _isExpanded;
      }
      set
      {
         if (_isExpanded== value)
            return;

         _isExpanded= value;
         OnPropertyChanged("IsExpanded");
      }
   }

   //NotifyPropertyChanged Implementation removed so the focus stays on problem...
}

public class CombinedItem : Item
{
   private readonly ObjectItem _objectItem = new ObjectItem();

   public bool IsExpanded
   {
      get { return _objectItem.IsExpanded; }
      set { _objectItem.IsExpanded = value; }
   }

   public static implicit operator ObjectItem(CombinedItem combinedItem)
   {
      return combinedItem._objectItem;
   }
}

I am now facing the problem that the Property IsExpanded doesnt get Notified to the UI correctly when I have a CominedItem as the DataContext, the IsVisible Property works as expected.

To overcome the problem I have changed the CominedItem to the following:

public class CombinedItem : Item
{
   private readonly ObjectItem _objectItem = new ObjectItem();

   public bool IsExpanded
   {
      get { return _objectItem.IsExpanded; }
      set 
      { 
         if (_objectItem.IsExpanded == value)
            return;

         _objectItem.IsExpanded = value;
         OnPropertyChanged("IsExpanded");
      }
   }

   public static implicit operator ObjectItem(CombinedItem combinedItem)
   {
      return combinedItem._objectItem;
   }
}

Is there a way to avoid writing the OnPropertyChanged("IsExpanded") again, with this approach. (I know there are libaries/tools, where you dont need to write it at all and just have to declare a attribute, pls dont suggest those)

Was it helpful?

Solution

Actually you should subscribe to ObjectItem PropertyChanged and raise the matching event on CombinedItem.

If _objectItem.IsExpanded is modified without using CombinedItem.IsExpanded, your UI will not see the change.

Without some magic attribute/tool if you want to wrap a property, you will have to handle changes notification.

public class CombinedItem : Item
{
   private readonly ObjectItem _objectItem = new ObjectItem();

   public CombinedItem()
   {
       _objectItem.PropertyChanged += (s, e) => 
       {
           if (e.PropertyName == "IsExpanded")
               OnPropertyChanged("IsExpanded");
       }
   }

   public bool IsExpanded
   {
      get { return _objectItem.IsExpanded; }
      set { _objectItem.IsExpanded = value; }
   }

   public static implicit operator ObjectItem(CombinedItem combinedItem)
   {
      return combinedItem._objectItem;
   }
}

OTHER TIPS

You could expose ObjectItem as a property and bind to that

public class CombinedItem : Item
{
   private readonly ObjectItem _objectItem = new ObjectItem();

   public bool IsExpanded
   {
      get { return _objectItem.IsExpanded; }
      set { _objectItem.IsExpanded = value; }
   }

   public ObjectItem ObjectItem
   {
      get { return _objectItem; }
   }

   public static implicit operator ObjectItem(CombinedItem combinedItem)
   {
      return combinedItem._objectItem;
   }
}



<Expander IsExpanded={Binding ObjectItem.IsExpanded}/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top