我的組合框有綁定更新問題。我的ComboBox的ItemSource綁定到一個LaneConfigurations列表。 ComboBox的SelectedItem綁定到我的代碼隱藏中的SelectedLaneConfiguration屬性。我配置了ComboBox的ItemTemplate以顯示每個LaneConfiguration的'DisplayID'屬性。綁定在ComboBox中不更新當底層ComboBox項目描述發生變化時的文本字段
這在大多數情況下都適用。但是,更改車道配置可能會導致DisplayID發生變化。如果您選擇了特定車道並且DisplayID顯示爲組合框的「文本」,然後更改了LaneConfiguration對象,則組合框的「文本」部分不會更新爲應顯示的新「DisplayID」 。當我單擊組合框上的下拉箭頭時,出現在列表中的選定項目的項目顯示正確的DisplayID,但與'Text'中顯示在組合框頂部的'DisplayID'不匹配'字段。
在我的代碼背後,我在'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#代碼也很有幫助。 – Tim
LaneConfigurations必須繼承INotifyPropertyChanged,你做到了嗎? –
顯示「改變LaneConfiguration對象」的含義 – Paparazzi