代码之家  ›  专栏  ›  技术社区  ›  Sergey Aldoukhov

在XAML中表示组合框或列表框的选项列表的好方法是什么?

  •  0
  • Sergey Aldoukhov  · 技术社区  · 16 年前

    任何东西都比

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

    ?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Echilon Mafarnakus    16 年前

    如果没有代码,那你就可以做到。

        2
  •  0
  •   Robert Rossney    14 年前
    <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 在运行时,如果需要的话。

    推荐文章