2014-01-23 19 views
1

我有以下XAML代碼片段在我的XMAL文件中出現多次。 是否可以將ComboBox和ComboBoxItems結合到一個靜態或動態資源中來節省空間並簡化維護問題?在我的XAML中,我可以將ComboBox和多個ComboBoxItems製作爲單個靜態或動態資源?

 <Button> 
      <Canvas HorizontalAlignment="Left" VerticalAlignment="Top"> 
       <ComboBox Width="34" FontSize="13" Margin="0" Padding="2,0,0,0"> 
        <ComboBoxItem Content="01"></ComboBoxItem> 
        <ComboBoxItem Content="02"></ComboBoxItem> 
        <ComboBoxItem Content="03"></ComboBoxItem> 
        <ComboBoxItem Content="04"></ComboBoxItem> 
        <ComboBoxItem Content="05"></ComboBoxItem> 
        <ComboBoxItem Content="06"></ComboBoxItem> 
        <ComboBoxItem Content="08"></ComboBoxItem> 
        ....... ALL THE WAY TO 100 Items......      
        <ComboBoxItem Content="100"></ComboBoxItem> 
       </ComboBox> 
      </Canvas>  
     </Button> 
+4

**解決方案:MVVM **。創建一個包含'List Numbers {get; set;}'並用'Numbers = Enumerable.Range(0,100).ToList();'填充的適當ViewModel,然後綁定你的UI,或者爲ComboBoxes創建一個樣式相應地設置了「ItemsSource」和「SelectedItem」屬性。 –

回答

0

這部分添加到您的資源字典...

<Style x:Key="NumberComboButton" TargetType="Button"> 
    <Setter Property="Width" Value="45"/> 
    <Setter Property="Height" Value="40"/> 
    <Setter Property="Background" Value="#FF1F3B53"/> 
    <Setter Property="Foreground" Value="#FF000000"/> 
    <Setter Property="Padding" Value="3"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="BorderBrush"> 
     <Setter.Value> 
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
       <GradientStop Color="#FFA3AEB9" Offset="0"/> 
       <GradientStop Color="#FF8399A9" Offset="0.375"/> 
       <GradientStop Color="#FF718597" Offset="0.375"/> 
       <GradientStop Color="#FF617584" Offset="1"/> 
      </LinearGradientBrush> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Grid> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"/> 
          <VisualState x:Name="MouseOver"> 
           <Storyboard> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="BackgroundAnimation" Storyboard.TargetProperty="Opacity" To="1"/> 
            <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" To="#F2FFFFFF"/> 
            <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" To="#CCFFFFFF"/> 
            <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)" To="#7FFFFFFF"/> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Pressed"> 
           <Storyboard> 
            <ColorAnimation Duration="0" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="#FF6DBDD1"/> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="BackgroundAnimation" Storyboard.TargetProperty="Opacity" To="1"/> 
            <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" To="#D8FFFFFF"/> 
            <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" To="#C6FFFFFF"/> 
            <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" To="#8CFFFFFF"/> 
            <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)" To="#3FFFFFFF"/> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity" To=".55"/> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
         <VisualStateGroup x:Name="FocusStates"> 
          <VisualState x:Name="Focused"> 
           <Storyboard> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity" To="1"/> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Unfocused" /> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Border x:Name="Background" CornerRadius="3" Background="White" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}"> 
         <Grid Background="{TemplateBinding Background}" Margin="1"> 
          <Border Opacity="0" x:Name="BackgroundAnimation" Background="#FF448DCA" /> 
          <Rectangle x:Name="BackgroundGradient" > 
           <Rectangle.Fill> 
            <LinearGradientBrush StartPoint=".7,0" EndPoint=".7,1"> 
             <GradientStop Color="#FFFFFFFF" Offset="0" /> 
             <GradientStop Color="#F9FFFFFF" Offset="0.375" /> 
             <GradientStop Color="#E5FFFFFF" Offset="0.625" /> 
             <GradientStop Color="#C6FFFFFF" Offset="1" /> 
            </LinearGradientBrush> 
           </Rectangle.Fill> 
          </Rectangle> 
          <ComboBox Width="34" FontSize="13" Margin="0" Padding="2,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center"> 
           <ComboBoxItem Content="01"></ComboBoxItem> 
           <ComboBoxItem Content="02"></ComboBoxItem> 
           <ComboBoxItem Content="03"></ComboBoxItem> 
           <ComboBoxItem Content="04"></ComboBoxItem> 
           <ComboBoxItem Content="05"></ComboBoxItem> 
           <ComboBoxItem Content="06"></ComboBoxItem> 
           <ComboBoxItem Content="08"></ComboBoxItem> 
           <ComboBoxItem Content="100"></ComboBoxItem> 
          </ComboBox> 
         </Grid> 
        </Border> 
        <ContentPresenter 
          x:Name="contentPresenter" 
          Content="{TemplateBinding Content}" 
          ContentTemplate="{TemplateBinding ContentTemplate}" 
          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          Margin="{TemplateBinding Padding}"/> 
        <Rectangle x:Name="DisabledVisualElement" RadiusX="3" RadiusY="3" Fill="#FFFFFFFF" Opacity="0" IsHitTestVisible="false" /> 
        <Rectangle x:Name="FocusVisualElement" RadiusX="2" RadiusY="2" Margin="1" Stroke="#FF6DBDD1" StrokeThickness="1" Opacity="0" IsHitTestVisible="false" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

,並在XAML中像這樣使用它......」

 <Button Style="{StaticResource NumberComboButton}"/>    

我已經編輯這篇提供完整的模板,而不僅僅是想法提供者。

我刪除了你的Canvas,因爲Canvases不會佈置它們的c承運。

爲了使按鈕高/寬則需要更改以下屬性:

<Setter Property="Width" Value="45"/> 

    <Setter Property="Height" Value="40"/> 

指定的XAML按鈕時,將覆蓋樣式指定的值設置這些值。

最後,要更改組合框在按鈕中的對齊方式,請更改組合框中的對齊方式 - 您可以在按鈕網格內(通過可視狀態管理器內容)找到組合框。

您可以在這裏瞭解更多...

http://msdn.microsoft.com/en-us/library/cc903963(v=vs.95).aspx - 如何自定義按鈕 http://msdn.microsoft.com/en-us/library/cc278069(v=vs.95).aspx - 按鈕模板

+0

這不會工作。組合框取代了整個按鈕的內部。我想讓按鈕的一部分保持不變,所以我可以單擊按鈕的外邊緣,或單擊ComboBox本身。 – user3130331

6

無需申報這麼多comboboxItem的實例。簡單地聲明的資源提供的ItemsSource到您的組合框(你可以使用的ObjectDataProvider

中聲明用戶控件,窗口或應用程序資源此資源(無論你覺得喜歡):

<ObjectDataProvider x:Key="EnumerableRange" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib" 
        xmlns:linq="clr-namespace:System.Linq;assembly=System.Core" 
        ObjectType="{x:Type linq:Enumerable}" MethodName="Range"> 
    <ObjectDataProvider.MethodParameters> 
     <sys:Int32>1</sys:Int32> 
     <sys:Int32>100</sys:Int32> 
    </ObjectDataProvider.MethodParameters> 
</ObjectDataProvider> 

,你可以用這個通過設置ItemsSource這樣的多個組合框:

<ComboBox ItemsSource="{Binding Source={StaticResource EnumerableRange}}"/> 
相關問題