1

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

这在大多数情况下都有效。但是,更改通道配置可能会导致 DisplayID 更改。如果您选择了一个特定的车道,并且它的 DisplayID 显示为 ComboBox 的“文本”,然后您更改了 LaneConfiguration 对象,则组合框的“文本”部分不会更新为应该显示的新“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);
          }
       }
   }
4

2 回答 2

1

我为此创建了一个较短的示例并重新发布并得到了答案

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

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

于 2012-06-15T19:10:49.323 回答
0

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

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

于 2012-06-18T06:54:52.147 回答