【问题标题】:UWP Exception when binding to ComboBox绑定到 ComboBox 时出现 UWP 异常
【发布时间】:2017-11-03 15:14:21
【问题描述】:

我遇到了这个异常:

AppName.exe 中出现未处理的 win32 异常

仅当我绑定到 ComboBox 时才会发生这种情况 这是我的 XAML:

<ContentDialog x:Name="BrowseBySourceDialog"                                   
                                   Grid.Column="1"
                                   PrimaryButtonText="Accept" IsPrimaryButtonEnabled="False"          
                                   Opened="BrowseBySourceDialog_Opened">

                        <StackPanel  Background="White">
                                <Border Background="DarkGreen" MinHeight="40"
                                           MinWidth="300">
                                    <TextBlock Text="Browse: By Source"                     
                                                VerticalAlignment="Center"
                                                Foreground="White"        
                                                TextAlignment="Center"
                                                HorizontalAlignment="Center" />
                                </Border>
                            <StackPanel>

                                <ComboBox x:Name="BrowseBySourceComboBox" 
                                          PlaceholderText="Select Source"                      
                                          Height="40" 
                                          Width="300" 
                                          ItemsSource="{x:Bind BrowseBySourceList, Mode=OneWay}" 
                                          DisplayMemberPath="Source">
                                    <ComboBox.ItemTemplate>
                                        <DataTemplate x:DataType="local:FeaturedResponseItem">
                                            <TextBlock Text="{x:Bind key}"/>
                                        </DataTemplate>
                                    </ComboBox.ItemTemplate>

                                </ComboBox>
                            </StackPanel>
                        </StackPanel>
                    </ContentDialog>

仅当存在以下行时才会发生此异常:

<ComboBox.ItemTemplate>
     <DataTemplate x:DataType="local:FeaturedResponseItem">
           <TextBlock Text="{x:Bind key}"/>
      </DataTemplate>
  </ComboBox.ItemTemplate>

如果删除以上行,组合框肯定保持为空,但不会发生异常。绑定错了吗?

来源:

ObservableCollection<FeaturedResponseItem> BrowseBySourceList { get; set; } = new ObservableCollection<FeaturedResponseItem>();

型号:

public class FeaturedResponseItem
    {
        public string key { get; set; }
        public string icon { get; set; }
        public bool accessible { get; set; }
    }

【问题讨论】:

  • Source的类型是什么?
  • @JustinXL 更新了我的问题

标签: xaml combobox uwp


【解决方案1】:

您可以删除 DisplayMemberPath="Source" 或直接删除 ItemTemplate 并将 DisplayMemberPath="Source" 更改为 DisplayMemberPath="Key"

【讨论】:

    猜你喜欢
    • 2017-01-05
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多