silverlight - 设置NumericUpDown的背景

标签 silverlight xaml

我有以下 XAML:

<toolkit:NumericUpDown Background="White"/>

但是,这不会更改控件的背景颜色。我已经尝试在 SpinnerTemplate 上分配背景,但这也不起作用。有什么方法可以让这个控件有背景而不必制作新模板吗?

最佳答案

无需制作新模板?我不这么认为。

这是否满足您需要完成的任务?

<toolkit1:NumericUpDown Background="Red" HorizontalAlignment="Left" VerticalAlignment="Top">
    <toolkit1:NumericUpDown.Resources>
        <Style x:Key="NumericUpDownStyle1" TargetType="toolkit1:NumericUpDown">
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Height" Value="22"/>
            <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="toolkit1:NumericUpDown">
                        <Grid Background="{TemplateBinding Background}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0"/>
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="DisabledVisualElement"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualState x:Name="Focused">
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualElement">
                                                <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Unfocused"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <toolkit1:ButtonSpinner x:Name="Spinner" HorizontalContentAlignment="Stretch" MinWidth="35" VerticalContentAlignment="Stretch">
                                <TextBox x:Name="Text" AcceptsReturn="False" BorderThickness="0" Foreground="{TemplateBinding Foreground}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontStretch="{TemplateBinding FontStretch}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" MinWidth="20" TextAlignment="Right" TextWrapping="NoWrap" Text="{TemplateBinding Value}">
                                    <TextBox.Style>
                                        <Style TargetType="TextBox">
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType="TextBox">
                                                        <ScrollViewer x:Name="ContentElement" BorderThickness="0" Padding="0"/>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </TextBox.Style>
                                </TextBox>
                            </toolkit1:ButtonSpinner>
                            <Border x:Name="DisabledVisualElement" Background="#A5FFFFFF" CornerRadius="2.5,2.5,2.5,2.5" IsHitTestVisible="false" Opacity="0"/>
                            <Border x:Name="FocusVisualElement" BorderBrush="#FF45D6FA" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1,1,1,1" IsHitTestVisible="False" Opacity="0"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </toolkit1:NumericUpDown.Resources>
    <toolkit1:NumericUpDown.Style>
        <StaticResource ResourceKey="NumericUpDownStyle1"/>
    </toolkit1:NumericUpDown.Style>
</toolkit1:NumericUpDown>

关键行是:

<Grid Background="{TemplateBinding Background}">

如果您打算经常这样做,可以将这种样式放在资源字典中。

关于silverlight - 设置NumericUpDown的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11716749/

相关文章:

silverlight - Silverlight 4 和 Windows Phone 7 上的多点触控手势

c# - 在 Expression Blend + Silverlight 中使用标注

c# - 计算机电话集成软电话架构问题

multithreading - 在非 UI 线程上运行 RIA 服务

wpf - 使用混合编辑单独资源文件中的样式

Silverlight 2.0 - 将域对象数据绑定(bind)到 UserControl

c# - 在 Windows8 中编写 C#/XAML 与 C++/XAML WinRT 应用程序的优缺点是什么?

c# - 将属性绑定(bind)到创建的每个 ListBox.ItemTemplate

c# - Windows 8 - 缓存分页按钮视觉状态在返回时不会改变

wpf - 什么时候应用 horizo​​ntalalignment =stretch?