2016-11-20 59 views
0

我创建了与ObservableCollection绑定的ComboBox。这是代码。无法在UWP中的ComboBox中选择项目

XAML

 <StackPanel HorizontalAlignment="Center" 
        Grid.Row="0"> 
      <TextBlock Text="Choose a city" /> 
      <ComboBox x:Name="CityComboBox" 
         SelectionChanged="CityComboBox_SelectionChanged" 
         ItemsSource="{x:Bind cities}" 
         HorizontalAlignment="Center" 
         VerticalAlignment="Top"> 
       <ComboBox.ItemTemplate> 
        <DataTemplate x:DataType="data:City"> 
         <ComboBoxItem Content="{x:Bind name}" /> 
        </DataTemplate> 
       </ComboBox.ItemTemplate> 
      </ComboBox> 
     </StackPanel> 

C#类

public class Grad 
    { 
     public String imeGrada { get; set; } 
     public ArrayList arr1 { get; set; } 
     public List<A> list1 { get; set; } 

     public City() 
     { 
      name = "NewYork"; 
      arr1= new ArrayList(); 
      list1 = new List<A>(); 
     } 

当我运行应用程序,我不能选择ComboBoxItem点击它的中心,但只能在该项目的优势。如果我使用ComboBox而不进行拼接,但它可以正常工作。

Image

选择仅在绿色区域中单击了工作。点击红色区域什么也不做。

是什么导致了这种情况,我该如何解决?

在此先感谢!

回答

1

由于DataTemplate的内容将自动添加到另一个ComboBoxItem中,因此您不应在DataTemplate中使用ComboBoxItem。在你的解决方案中有另一个ComboBoxItem中的ComboBoxItem。

因此,对于你的问题的解决方案应该是这样的:

<ComboBox.ItemTemplate> 
    <DataTemplate x:DataType="data:City"> 
     <TextBlock Text="{x:Bind name}" /> 
    </DataTemplate> 
</ComboBox.ItemTemplate>