在我的XAML中,我可以将ComboBox和多个ComboBoxItem组合成一个静态或动态资源吗?

时间:2014-01-23 17:45:00

标签: c# wpf xaml combobox

我在XMAL文件中多次出现以下XAML片段。 是否可以将ComboBox和ComboBoxItem组合成一个静态或动态资源,以节省空间并简化维护问题?

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

2 个答案:

答案 0 :(得分:6)

无需声明如此多的comboboxItem实例。只需声明资源以向您的comboBox提供ItemsSource(您可以使用 ObjectDataProvider

在UserControl,Window或App资源中声明此资源(无论您在何处):

<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}}"/>

答案 1 :(得分: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}"/>            

我已对此进行了编辑,以提供完整的模板,而不仅仅是创意提供者。

我删除了你的画布,因为画布不会布置他们的孩子。

要使按钮更高/更宽,您需要更改以下属性:

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

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

在XAML中指定Button时设置这些值将覆盖样式中指定的值。

最后,要更改组合框在按钮中的对齐方式,更改组合框中的对齐方式 - 您可以在按钮的网格中找到组合框(通过可视状态管理器的东西)。

你可以在这里了解更多...

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