是否有更短的方法来编写这个XAML?

时间:2013-06-24 07:34:13

标签: xaml

是否有更短的编写方式,比如在ItemTemplate声明中使用属性ComboBox?我讨厌查看我的代码并看到这一大块代码。

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

1 个答案:

答案 0 :(得分:2)

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

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