组合框:在代码隐藏中设置 SelectedIndex 后,第一项出现绑定错误
本文关键字:一项 错误 绑定 代码 隐藏 SelectedIndex 设置 组合 | 更新日期: 2023-09-27 18:35:06
我真的疯了。我有一个漂亮的组合框,里面装满了我的调用项目...绑定效果很好,但是在代码中设置Combobox.SelectedIndex = "0"
后,显示第一个条目时出现错误。(组合框选择字段保持为空)
-
我的组合框的 Xaml
<PivotItem> <PivotItem.Header> <TextBlock x:Uid="section1"/> </PivotItem.Header> <Grid Grid.Row="1"> <ComboBox x:Name="cb_arbeitsplatz" x:Uid="arbeitsplatz" Header="Wähle Arbeitsplatz" ItemsSource="{Binding}" Margin="0,6,0,0" SelectionChanged="cb_arbeitsplatz_SelectionChanged" > <ComboBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding BEZEICH}" Style="{StaticResource HeaderTextStyle}"/> <TextBlock Text="{Binding ARBPLATZ}"/> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </Grid> </PivotItem>
-
那是我的代码
public ObservableCollection<arbeitsplatz_class> cb_viewmodel = new ObservableCollection<arbeitsplatz_class>(); public _04_request() { DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait | DisplayOrientations.Landscape; this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Required; this.navigationHelper = new NavigationHelper(this); this.navigationHelper.LoadState += this.NavigationHelper_LoadState; this.navigationHelper.SaveState += this.NavigationHelper_SaveState; } public NavigationHelper NavigationHelper { get { return this.navigationHelper; } } private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e) { await JSON_arbeitsplatz.get_JSON("", cb_viewmodel); cb_arbeitsplatz.DataContext = cb_viewmodel; cb_arbeitsplatz.SelectedIndex = 0; ... }
-
这是我从VisualStudio得到的错误。而且它仅适用于第一项。其余所有工作都很好:
Error: BindingExpression path error: 'BEZEICH' property not found on 'System.Collections.ObjectModel.ObservableCollection`1[[GANTERportal.Data.arbeitsplatz_class, GANTERportal, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'. BindingExpression: Path='BEZEICH' DataItem='System.Collections.ObjectModel.ObservableCollection`1[[GANTERportal.Data.arbeitsplatz_class, GANTERportal, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String') Error: BindingExpression path error: 'ARBPLATZ' property not found on 'System.Collections.ObjectModel.ObservableCollection`1[[GANTERportal.Data.arbeitsplatz_class, GANTERportal, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'. BindingExpression: Path='ARBPLATZ' DataItem='System.Collections.ObjectModel.ObservableCollection`1[[GANTERportal.Data.arbeitsplatz_class, GANTERportal, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')
-
我可以选择第一项。当我点击组合框时,它正在嘘寒问暖很遗憾我无法发送图片
-
但是,由于绑定错误,选择第一项将导致空白组合框。同样,这只是当我从代码隐藏中设置组合框的选择时...????很遗憾我无法发送图片
事实证明,第二个列表视图导致了所有的混乱。我也设置了列表视图的选定索引(没有返回错误消息),但这导致了麻烦。删除它 - 一切正常。现在我需要找到一个解决方案,如何激活组合框和列表视图......
对于它的价值,这里有几件事我会检查/做:
a) 进行检查以确保在以下行之前填充 ComboBox:Combobox.SelectedIndex = "0"b) 如果可能,在 CombBox 报告已完成其数据绑定操作时设置默认选择。c) 作为测试,尝试以下一个或两个 a) 默认为另一个索引以消除它是第一项特有的东西的可能性 b) 看看你是否可以在屏幕加载时从列表中选择索引零。