ComboBox SelectedItem在Windows 10中引发异常

时间:2015-08-02 06:27:58

标签: xaml windows-runtime windows-store-apps winrt-xaml windows-10

以下代码在Windows 8.1下正常运行 我最近将我的操作系统升级到了Windows 10,它引发了异常。

以下是该例外的屏幕截图 enter image description here

这是XAML代码:

  <ComboBox Grid.Row="3" HorizontalContentAlignment="Left" HorizontalAlignment="Left"
                      TabIndex="2" VerticalAlignment="Center"
                      ItemsSource="{Binding DateFormats}"
                      SelectedItem="{Binding DateFormatSelected, Mode=TwoWay}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Value}" />
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

当我删除部件&#34; SelectedItem&#34;它工作,但我需要SelectedItem 要显示。 请帮忙。

此处是事件查看器应用程序错误详细信息。它没有提供太多信息

  

错误应用程序名称:BoardPACWinApp.exe,版本:0.0.0.1,时间戳:0x55bdb705   错误模块名称:Windows.UI.Xaml.dll,版本:10.0.10240.16397,时间戳:0x55af0da4   异常代码:0xc000027b   故障偏移:0x00722f90   错误进程id:0x1e48   错误应用程序启动时间:0x01d0cd2970f53bb1   错误应用程序路径:D:\ Projects \ BoardPACWinApp \ BoardPACWinApp \ bin \ x86 \ Debug \ AppX \ BoardPACWinApp.exe   错误模块路径:C:\ Windows \ System32 \ Windows.UI.Xaml.dll   报告编号:38e21b5c-cf75-4849-81df-01bb412c291a   错误包全名:IronOneTechnologiesPvtLtd.BoardPACWinDemo_3.14.35.2_x86__na7z394ep8t7e   错误的包相关应用程序ID:App

2 个答案:

答案 0 :(得分:1)

我遇到了类似的异常,这是因为我在绑定过程开始时操作了ItemsSource绑定属性(List&lt;&gt;)。现在我对临时集合进行所有操作,当对集合的所有操作都完成时,我将其分配给绑定属性。

希望这会有所帮助......

答案 1 :(得分:0)

我所做的是使用代码视图执行SelectedItem绑定。

这是我的XAML代码:

<ComboBox x:Name="DateFormatsComboBox" Grid.Row="3" Style="{StaticResource CustomBlueComboBoxStyle}"
                  ItemsSource="{Binding DateFormats}" ItemContainerStyle="{StaticResource CustomBlueComboBoxItemStyle}">
            <!--NOTE: Removed coz Windows 10 had issues | 2015-08-16 | SurenM -->
            <!--SelectedItem="{Binding DateFormatSelected, Mode=TwoWay}"-->
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Value}" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

这是我的C#代码:

//NOTE: Windows 10 had issues with XAML Combobox SelectItem, so it was brought to code behind
            DateFormatsComboBox.SelectedItem = _model.DateFormatSelected;

注意:_model.DateFormatSelected是CustomKeyValuePair<string, string>

这种方法在Windows 8.1和Windows 10上均经过测试,效果很好。