ComboBox: 在代码中设置SelectedIndex后,第一项绑定出错。

3

我真的快疯了。我有一个很好的组合框,里面填满了我的CallsItems...绑定非常好,但是在代码后端设置Combobox.SelectedIndex = "0"之后,显示第一个条目时出错。(组合框选项字段保持为空)

  1. My Xaml of the Combobox

    <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. That's my code

    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. This is the error I get from VisualStudio. And it's only for the first Item. All the Rest works pretty fine:

    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. I can pick the first item. It's shwoing when I'm clicking on the Combobox Unfortunately I can't send pictures

  5. But choosing the first item will lead to a blank Combobox, due to binding Errors. Again that's only when I'm Setting the Selection of the Combobox from code behind...???? Unfortunately I can't send pictures

2个回答

1

结果发现第二个ListView导致了所有混乱。我也设置了Listview的SelectedIndex(没有错误信息返回),但这导致了问题。删除它 - 一切都可以正常工作。现在我需要找到一个解决方案来同时激活Combobox和Listview...


@McGarnagle:你能告诉我需要使用哪个Using指令吗?它能在WindowsPhone 8.1(RT而非Silverlight)上运行吗?另外,我无法评论你的答案?! - thezapper

0

就我个人而言,这是我会检查/执行的几件事:

a) 在这行代码之前加入一个检查,以确保ComboBox已经被填充:Combobox.SelectedIndex = "0" b) 如果可能的话,在CombBox报告完成数据绑定操作时设置默认选择。 c) 作为测试,尝试执行以下任一或两个步骤:a)将默认值更改为另一个索引,以消除可能是第一个项目特有的问题;b)查看是否可以在屏幕加载后从列表中选择索引零。


@maltpm: 我已经做过了。什么都没有改变。我将 theSelectedIndex 设置为任何数字。它总是不显示第一个项目。我在 Combobox.Loaded 中还设置了一个 bool,以避免在所有内容填充之前设置索引。但是仍然没有改变。我还是有这种奇怪的行为?! - thezapper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接