2013-06-24 118 views
2

是否有任何更短的寫法,如使用ComboBox聲明中的屬性ItemTemplate?我討厭看我的代碼,看到這個龐大的代碼。有沒有更簡單的方法來編寫這個XAML?

<ComboBox Grid.Row="0" Grid.Column="1" Margin="3" ItemsSource="{Binding Accounts}" SelectedItem="{Binding SelectedAccount}" > 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <ComboBoxItem Content="{Binding Name}" /> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

回答

2

如果您只想顯示項目的名稱,您可以使用組合框的DisplayMemberPath-Property。然後您將組合框定義爲:

<ComboBox Grid.Row="0" 
      Grid.Column="1" 
      Margin="3" 
      ItemsSource="{Binding Accounts}" 
      SelectedItem="{Binding SelectedAccount}" 
      DisplayMemberPath="Name"/> 
相關問題