C# 当基础组合框项说明更改时,组合框文本字段中的绑定未更新
我的组合框有绑定更新问题。我的组合框的ItemSource绑定到LaneConfigurations列表。组合框的SelectedItem绑定到my code behind中的SelectedLaneConfiguration属性。我将组合框的ItemTemplate配置为显示每个LaneConfiguration的“DisplayID”属性 这在大多数情况下都有效。但是,更改车道配置可能会导致DisplayID更改。如果您选择了一个特定的车道,并且它的DisplayID显示为组合框的“文本”,然后您更改了LaneConfiguration对象,则组合框的“文本”部分不会用应显示的新“DisplayID”更新。当我单击组合框上的下拉箭头时,列表中显示为所选项目的项目显示了正确的DisplayID,但与组合框顶部“文本”字段中显示的“DisplayID”不匹配 在我的代码隐藏中,我正在对“SelectedLaneConfiguration”属性触发PropertyChanged事件。如何让ComboBox意识到“DisplayID”属性需要更新?我尝试为'DisplayID'属性触发PropertyChanged事件,但它是LaneConfiguration类的一部分,因此它似乎没有更新 我已经包括了下面的XAML以及一个屏幕截图,显示ComboBox文本显示与ComboBox下拉列表的内容不同步 部分XAML:C# 当基础组合框项说明更改时,组合框文本字段中的绑定未更新,c#,wpf,xaml,binding,wpf-controls,C#,Wpf,Xaml,Binding,Wpf Controls,我的组合框有绑定更新问题。我的组合框的ItemSource绑定到LaneConfigurations列表。组合框的SelectedItem绑定到my code behind中的SelectedLaneConfiguration属性。我将组合框的ItemTemplate配置为显示每个LaneConfiguration的“DisplayID”属性 这在大多数情况下都有效。但是,更改车道配置可能会导致DisplayID更改。如果您选择了一个特定的车道,并且它的DisplayID显示为组合框的“文本”,
<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>
我创建了一个较短的例子,并重新发布,获得了一个 我想我只需要在代码背后更改InotifyProperty。实际上,我在数据对象LaneConfiguration类中也需要它。我应该更加关注阿里·阿德尔的评论。这会让我少受一天的挫折
感谢Ali Adl和Tim提供的有用答案 我创建了一个较短的示例,并重新发布,获得了一个 我想我只需要在代码背后更改InotifyProperty。实际上,我在数据对象LaneConfiguration类中也需要它。我应该更加关注阿里·阿德尔的评论。这会让我少受一天的挫折
感谢Ali Adl和Tim提供的有用答案 正确的方法是使您绑定的属性-dependencProperty-自动更新不会有任何问题
如果您使用INotifyPropertyCahgnge,那么它必须工作,如果它不工作,那么请检查PropertyChanged event==null。正确的方法是使您绑定的属性-g-DependencyProperty-自动更新不会有任何问题
如果您使用INotifyPropertyCahgnge,那么它必须工作,如果它不工作,请检查PropertyChanged event==null。查看一些C代码也会很有帮助。LaneConfigurations必须继承INotifyPropertyChanged,是吗?显示“更改LaneConfiguration对象”的含义我继承并实现了INotifyPropertyChanged,并使用它在SelectedLaneConfiguration更改时发出通知,但我似乎不知道如何在SelectedLaneConfiguration的“DisplayID”属性更改时通知。“更改LaneConfiguration对象”意味着我更改了该对象上的属性,这将导致“DisplayID”文本更改。原来的车道数是10,所以DisplayID属性返回'Lanes 1-10',当我将车道数更改为7时,DisplayID属性将返回'Lanes 1-7'。问题是我不知道如何告诉组合框“SelectedLaneConfiguration”的“DisplayID”需要更新。查看一些C代码也会有所帮助。LaneConfigurations必须继承INotifyPropertyChanged,是吗?显示“更改LaneConfiguration对象”的意思我继承并实现了INotifyPropertyChanged,并使用它在SelectedLaneConfiguration更改时发出通知,但我似乎不知道如何在SelectedLaneConfiguration的“DisplayID”属性更改时通知。“更改LaneConfiguration对象”意味着我更改了该对象上的属性,这将导致“DisplayID”文本更改。原来的车道数是10,所以DisplayID属性返回'Lanes 1-10',当我将车道数更改为7时,DisplayID属性将返回'Lanes 1-7'。问题是我不知道如何告诉组合框“SelectedLaneConfiguration”的“DisplayID”需要更新。
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);
}
}
}