【问题标题】:Binding not updating in ComboBox Text field when underlying ComboBox item description changes当底层组合框项目描述更改时,组合框文本字段中的绑定不更新
【发布时间】:2012-06-17 20:59:26
【问题描述】:

我的 ComboBox 出现绑定更新问题。我的 ComboBox 的 ItemSource 绑定到 LaneConfigurations 列表。 ComboBox 的 SelectedItem 绑定到我的代码隐藏中的 SelectedLaneConfiguration 属性。我将 ComboBox 的 ItemTemplate 配置为显示每个 LaneConfiguration 的“DisplayID”属性。

这在大多数情况下都有效。但是,更改通道配置可能会导致 DisplayID 更改。如果您选择了一个特定的车道,并且它的 DisplayID 显示为 ComboBox 的“文本”,然后您更改了 LaneConfiguration 对象,则 ComboBox 的“文本”部分不会更新为应该显示的新“DisplayID” .当我单击组合框上的下拉箭头时,在列表中显示为所选项目的项目显示正确的 DisplayID,但这与组合框顶部的“文本”中显示的“显示 ID”不匹配' 字段。

在我后面的代码中,我在“SelectedLaneConfiguration”属性上触发了一个 PropertyChanged 事件。如何让 ComboBox 意识到“DisplayID”属性需要更新?我尝试为“DisplayID”属性触发 PropertyChanged 事件,但它是 LaneConfiguration 类的一部分,因此它似乎没有更新。

我已包含下面的 XAML 以及显示 ComboBox 文本显示与 ComboBox 下拉列表内容不同步的屏幕截图。

部分 XAML:

<ComboBox x:Name="_comboBoxLanes" Height="26"
          ItemsSource="{Binding SensorConfiguration.LaneConfigurations}" 
          SelectedItem="{Binding Path=SelectedLaneConfiguration}">
     <ComboBox.ItemTemplate>
        <DataTemplate>
           <TextBlock Text="{Binding DisplayID, Mode=OneWay}"/>
        </DataTemplate>
     </ComboBox.ItemTemplate>
</ComboBox>

一些代码隐藏:

    public partial class LaneManagement : INotifyPropertyChanged, IDisposable
    {
        ..
        ..

        public event PropertyChangedEventHandler PropertyChanged;

        private void FirePropertyChanged(string prop)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }

        private void SensorConfiguration_Changed()
        {
            LaneTools.ResetLanePolygons();
            GenerateLaneTypeDropdowns();
            FirePropertyChanged("SensorConfiguration");
            FirePropertyChanged("SelectedLaneConfiguration");
            FirePropertyChanged("DisplayID");
        }
   }


   public class LaneConfiguration : PolygonConfiguration
   {
       public override string DisplayID
       {
          get
          {
              return IsLaneGroup?string.Format("Lanes {0} - {1}", ID, ID + LaneCount - 1):                                  string.Format("Lane {0}", ID);
          }
       }
   }

【问题讨论】:

  • 看看一些 C# 代码会很有帮助。
  • LaneConfigurations 必须继承 INotifyPropertyChanged,你做到了吗?
  • 通过“更改 LaneConfiguration 对象”说明您的意思
  • 我继承并实现了 INotifyPropertyChanged 并使用它在 SelectedLaneConfiguration 更改时发出通知,但我似乎无法弄清楚如何在 SelectedLaneConfiguration 的“DisplayID”属性发生更改时发出通知。
  • '改变 LaneConfiguration 对象'意味着我改变了它的一个属性,这应该导致'DisplayID'文本改变。所以旧的车道数为 10,因此 DisplayID 属性返回“车道 1-10”,当我将车道数更改为 7 时,DisplayID 属性将返回“车道 1-7”。问题是我不知道如何告诉 ComboBox 需要更新“SelectedLaneConfiguration”的“DisplayID”。

标签: c# wpf xaml binding wpf-controls


【解决方案1】:

我为此创建了一个较短的示例并重新发布并获得了answer

我以为我只需要代码隐藏中的 INotifyPropertyChanged。我实际上在我的数据对象 LaneConfiguration 类中也需要它。我应该更加关注 Ali Adl 的评论。它会为我节省一天的挫败感..

感谢 Ali Adl 和 Tim 的有用回答!

【讨论】:

    【解决方案2】:

    正确的方法是让你绑定的属性 - DependencyProperty - 自动更新不会有任何问题。

    如果您使用 INotifyPropertyCahgnge,则它必须工作,如果没有 - 检查 PropertyChanged 事件是否 == null。

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      相关资源
      最近更新 更多