我的 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);
}
}
}