Какой хороший способ представить список вариантов для поля со списком или list box в XAML?

StackOverflow https://stackoverflow.com/questions/1401167

Вопрос

Все, что более компактно (или в остальном лучше), чем

    <x:Array x:Key="titles" Type="System:String">
        <System:String>Mr.</System:String>
        <System:String>Mrs.</System:String>
        <System:String>Ms.</System:String>
    </x:Array>

?

Это было полезно?

Решение

Без кода это самое разумное, что вы можете придумать.

Другие советы

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
    <XmlDataProvider x:Key="Lookups">
      <x:XData>
        <ComboBoxItems xmlns="">
          <Salutations>
            <Item>Mr.</Item>
            <Item>Mrs.</Item>
            <Item>Ms.</Item>
          </Salutations>
          <States>
            <Item>AL</Item>
            <Item>AK</Item>
            <Item>CA</Item>
            <Item>CT</Item>
          </States>
          <Wizards>
            <Item>Gandalf</Item>
            <Item>Radagast</Item>
            <Item>Pallando</Item>
            <Item>Saruman</Item>
          </Wizards>
        </ComboBoxItems>
      </x:XData>
    </XmlDataProvider>
  </Page.Resources>
  <StackPanel>  
    <ComboBox ItemsSource="{Binding Source={StaticResource Lookups}, XPath=ComboBoxItems/Salutations/*}"/>
    <ComboBox ItemsSource="{Binding Source={StaticResource Lookups}, XPath=ComboBoxItems/States/*}"/>
    <ComboBox ItemsSource="{Binding Source={StaticResource Lookups}, XPath=ComboBoxItems/Wizards/*}"/>
  </StackPanel>
</Page>

Преимущество этого подхода заключается в том, что вы можете поддерживать элементы полностью отдельно от XAML, если вам нужно - вы можете сохранить их во внешнем XML-документе и загрузить XmlDataProvider во время выполнения, если вам нужно.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top