2015-05-14 49 views
0

我有多个可选择的组合框。因为它主要是重复代码,所以我想知道是否有一种方法可以创建一个资源或模板,我可以在其中设置ComboBoxItem一次,并且每次我想要一个Combobox都具有相同的项目时引用该键。XAML:为ComboBoxItems创建资源或模板

<ComboBox x:Name="CB1"> 
    <ComboBoxItem>SomeItem0</ComboBoxItem> 
    <ComboBoxItem>SomeItem1</ComboBoxItem> 
    <ComboBoxItem>SomeItem2</ComboBoxItem> 
    <ComboBoxItem>SomeItem3</ComboBoxItem> 
    <ComboBoxItem>SomeItem4</ComboBoxItem> 
    <ComboBoxItem>SomeItem5</ComboBoxItem> 
    <ComboBoxItem>SomeItem6</ComboBoxItem> 
    <ComboBoxItem>SomeItem7</ComboBoxItem> 
    <ComboBoxItem>SomeItem8</ComboBoxItem> 
    <ComboBoxItem>SomeItem9</ComboBoxItem> 
    <ComboBoxItem>SomeItem10</ComboBoxItem> 
    <ComboBoxItem>SomeItem11</ComboBoxItem> 
    <ComboBoxItem>SomeItem12</ComboBoxItem> 
    <ComboBoxItem>SomeItem13</ComboBoxItem> 
    <ComboBoxItem>SomeItem14</ComboBoxItem> 
    <ComboBoxItem>SomeItem15</ComboBoxItem> 
    <ComboBoxItem>SomeItem16</ComboBoxItem> 
    <ComboBoxItem>SomeItem17</ComboBoxItem> 
    <ComboBoxItem>SomeItem18</ComboBoxItem> 
    <ComboBoxItem>SomeItem19</ComboBoxItem> 
    <ComboBoxItem>SomeItem20</ComboBoxItem> 
<ComboBox> 

<ComboBox x:Name="CB2"> 
    <!--Same Items as above--> 
<ComboBox> 

<ComboBox x:Name="CB"> 
    <!--Same Items as above--> 
<ComboBox> 

. 
. 
. 
+3

将它们绑定ItemsSource属性的集合在您的视图模型(或代码背后) – Tuco

回答

1

,你可以在你的资源字典添加XmlDataProvider

<Window.Resources> 
    <XmlDataProvider x:Key="Collection" XPath="/COLLECTION"> 
    <x:XData> 
     <COLLECTION xmlns=""> 
      <ITEM>Item1</ITEM> 
      <ITEM>Item2</ITEM> 
      <ITEM>Item3</ITEM> 
      <ITEM>Item4</ITEM> 
     </COLLECTION> 
    </x:XData> 
</XmlDataProvider> 
</Window.Resources> 

然后绑定到它与组合框

<ComboBox Height="25" Width="100" ItemsSource="{Binding Source={StaticResource Collection},XPath=ITEM}" />