简体   繁体   中英

Binding Update of a Read-Only Property in DataTemplate

I have a ListBox that is bound, via dependency property, to a ObservableCollection<object> . I am using a DataTemplateSelector to determine which type of objects, based off of class type, are found in the ObservableCollection then I apply the appropriate DataTemplate. Multiple objects of any class type is possible.

Part of my model class is below. It has been edited to show points of interest only:

public class IPUpdater
{
   public IPUpdater()
   {
   }

   public string IPTransceiverInstall { get; set; }
   public string IPTransceiverFinal { get; set; }
   public Boolean IsFinal { get; set; }       

   public string IPTransceiver
   {
       get
       {
           return IPTransceiverAddress();
       }
   }

   private string IPTransceiverAddress()
   {
       if (!IsFinal)
           return IPTransceiverInstall;
       else
           return IPTransceiverFinal;
   }

}

IPTransceiver is bound to a TextBox within the XAML DataTemplate when the appropriate IPUpdater template is used. IsFinal is bound to a CheckBox within the same template.

Question: How do I force the TextBox to register that IPTransceiver has changed? I thought about trying to set a DependencyProperty equal to IPTransceiver, but I wasn't sure how to initially set it's value if there's more than one instance of the class in the Listbox.

TwoWay and OneWayToSource binding modes are not available for a read-only property. I can verify that IsFinal updates when the CheckBox value has changed. I've even tried to force IPTransceiverAddress() to trigger when IsFinal updates. None of the above have caused the TextBox to update. The only way I have been able to get the box to update is by repeating the SQLite Query, which I'd like to avoid.

Thanks!

Any classes you expect to use as binding sources should implement INotifyPropertyChanged . Your ViewModels (as well as your models, if you plan to bind the Views directly to them) should implement this interface and raise the PropertyChanged event in order for the WPF binding engine to reflect these changes in the UI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM