简体   繁体   English

DataTemplate 中只读属性的绑定更新

[英]Binding Update of a Read-Only Property in DataTemplate

I have a ListBox that is bound, via dependency property, to a ObservableCollection<object> .我有一个 ListBox,它通过依赖属性绑定到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.我正在使用DataTemplateSelector来确定在 ObservableCollection 中找到的基于类类型的对象类型,然后应用适当的 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.当使用适当的 IPUpdater 模板时, IPTransceiver绑定到 XAML DataTemplate 中的 TextBox。 IsFinal is bound to a CheckBox within the same template. IsFinal绑定到同一模板中的 CheckBox。

Question: How do I force the TextBox to register that IPTransceiver has changed?问题:如何强制 TextBox 注册IPTransceiver已更改? 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.我想尝试将 DependencyProperty 设置为等于 IPTransceiver,但是如果列表框中有多个类的实例,我不确定最初如何设置它的值。

TwoWay and OneWayToSource binding modes are not available for a read-only property. TwoWayOneWayToSource绑定模式不可用于只读属性。 I can verify that IsFinal updates when the CheckBox value has changed.当 CheckBox 值更改时,我可以验证IsFinal是否更新。 I've even tried to force IPTransceiverAddress() to trigger when IsFinal updates.我什至试图强制IPTransceiverAddress()IsFinal更新时触发。 None of the above have caused the TextBox to update.以上均未导致 TextBox 更新。 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.我能够让框更新的唯一方法是重复 SQLite 查询,我想避免这种情况。

Thanks!谢谢!

Any classes you expect to use as binding sources should implement INotifyPropertyChanged .您希望用作绑定源的任何类都应实现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.您的 ViewModel(以及您的模型,如果您计划将视图直接绑定到它们)应实现此接口并引发PropertyChanged事件,以便 WPF 绑定引擎在 UI 中反映这些更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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