繁体   English   中英

ComboBox:在后面的代码中设置SelectedIndex后,第一项的绑定错误

[英]ComboBox: Binding Error with first item after setting SelectedIndex in code behind

我真的变得疯狂了。 我有一个很好的Combobox填充我的CallsItems ...绑定工作得很好,但在代码后面设置Combobox.SelectedIndex = "0"后,我在显示第一个条目时出错。 (Combobox Selectionfield保持空白)

  1. 我的Combobox 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> 
  2. 那是我的代码

     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; ... } 
  3. 这是我从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') 
  4. 我可以选择第一项。 当我点击Combobox时它很震撼不幸的是我无法发送图片

  5. 但是由于绑定错误,选择第一个项目将导致空白的Combobox。 再一次,只有当我从后面的代码设置Combobox的选择时... ???? 不幸的是我无法发送图片

我发现第二个ListView引起了所有混乱。 我也设置了Listview的SelectedIndex(没有返回错误消息),但这导致了麻烦。 删除 - 并且一切正常。 现在我需要找到一个如何激活Combobox和Listview的解决方案......

对于它的价值,这里有一些我要检查/做的事情:

a)进行检查以确保在行之前填充ComboBox:Combobox.SelectedIndex =“0”b)如果可能,在CombBox报告完成它的数据绑定操作时设置默认选择。 c)作为测试,尝试a)默认为另一个索引中的一个或两个,以消除它是第一个项目特有的可能性b)看看你是否可以在屏幕加载时从列表中选择索引零。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM