2013-06-24 122 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"/> 
相关问题