c# - 从代码隐藏设置 silverlight 控件的视觉元素?

标签 c# silverlight xaml code-behind

我正在 silverlight 中开发一个按钮控件,它可以处于各种状态。每个状态都有自己的外观、不同的颜色,甚至按钮上的图像也不同。

我想知道如何从代码隐藏中修改控件的视觉属性?

这是 xaml:

<Style x:Key="MyButton" TargetType="Button">
            <Setter Property="FontSize" Value="10"/>
            <Setter Property="Height" Value="22"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid Name="RootElement">
                            <vsm:VisualStateManager.VisualStateGroups>
                                <vsm:VisualStateGroup x:Name="CommonStates">
                                    <vsm:VisualState x:Name="Normal"/>
                                    <vsm:VisualState x:Name="MouseOver">
                                    </vsm:VisualState>
                                    <vsm:VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:01.0010000" Storyboard.TargetName="border" Storyboard.TargetProperty="(UIElement.RenderTransform). (TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
                                                <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.94"/>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:01.0010000" Storyboard.TargetName="border" Storyboard.TargetProperty="(UIElement.RenderTransform). (TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
                                                <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.94"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </vsm:VisualState>
                                    <vsm:VisualState x:Name="Disabled">
                                        <Storyboard/>
                                    </vsm:VisualState>
                                </vsm:VisualStateGroup>
                                <vsm:VisualStateGroup x:Name="FocusStates">
                                    <vsm:VisualState x:Name="Focused">
                                        <Storyboard/>
                                    </vsm:VisualState>
                                    <vsm:VisualState x:Name="Unfocused"/>
                                </vsm:VisualStateGroup>
                            </vsm:VisualStateManager.VisualStateGroups>
                            <Border CornerRadius="1,1,1,1" Background="{StaticResource BlueVerticalGradientBrush}" BorderBrush="Red" BorderThickness="1,1,1,1" x:Name="MouseOver" RenderTransformOrigin="0.5,0.5">
                                <Border.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Border.RenderTransform>
                                <ContentPresenter x:Name="contentPresenter" Margin="10,0,10,0" ContentTemplate="{TemplateBinding ContentTemplate}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="MinWidth" Value="65"/>
        </Style>

最佳答案

您是否尝试过提供元素和 x:Name 并使用名称访问代码中的属性?

关于c# - 从代码隐藏设置 silverlight 控件的视觉元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5864535/

相关文章:

c# - 将 XAML 网格绑定(bind)到数组

wpf - 无法使用 WPF 设计器,因为无法在 XAML 中找到自定义基本窗口类型,但后面的代码很好

c# - 如何在代码隐藏中使用 wpflocalizeextension?

silverlight - Silverlight 3 中的剪贴板支持

silverlight - WP7 : How to do some treatment during splash screen, 但在第一页打开之前?

silverlight - Silverlight 如何在 Web 浏览器中工作?

c# - 如何在运行时修改xaml资源?

javascript - 由 MVC 呈现的对话框(弹出窗口) Bootstrap

c# - C++ 函数到 C#

c# - 没有得到这个继承