2

Are there perhaps any shorter ways of writing this, like using the property ItemTemplate in the ComboBox declaration? I hate looking at my code and seeing this big blob of code.

<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>
4

1 回答 1

2

如果您只想显示项目的名称,您可以使用 ComboBox 的DisplayMemberPath-Property。然后将 ComboBox 定义为:

<ComboBox Grid.Row="0" 
          Grid.Column="1"
          Margin="3" 
          ItemsSource="{Binding Accounts}" 
          SelectedItem="{Binding SelectedAccount}" 
          DisplayMemberPath="Name"/>
于 2013-06-24T07:45:00.873 回答