2016-11-20 60 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>