【问题标题】:Setting WPF Button Style Programmatically以编程方式设置 WPF 按钮样式
【发布时间】:2017-05-08 09:50:51
【问题描述】:

我有以下风格:

<Window.Resources>
    <Style x:Key="RoundCorner" TargetType="{x:Type Button}">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border CornerRadius="0,0,4,4" BorderBrush="black" 
     BorderThickness="1,0,1,1" >
                        <ContentPresenter HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Margin="1"></ContentPresenter>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="LayoutTransform">
            <Setter.Value>
                <RotateTransform CenterX="0.5" CenterY="0.5" Angle="270" />
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

我将它设置为这样的按钮

<Button x:Name="btn_bfx" ToolTip="BreakFix" Content="BFX" Click="btn_bfx_Click" Height="30" Width="40" HorizontalContentAlignment="Center" ></Button>

在构造函数中,我将样式设置为:

btn_bfx.Style = this.Resources["RoundCorner"] as Style;

稍后根据某些情况我想更改样式。

代码如下:

Style oldStyle = FindResource("RoundCorner") as Style;
Style newStyle = new Style();
newStyle.BasedOn = oldStyle;
newStyle.TargetType = typeof(Button);
foreach (var setter in oldStyle.Setters.ToList())
{
    newStyle.Setters.Add(setter);
}
newStyle.Setters.Add(new Setter(BackgroundProperty, Brushes.CadetBlue));
btn_bfx.Style = newStyle;

newStyle 只是为 oldStyle 添加背景颜色。但这种新风格并没有被应用。我仍然得到没有背景颜色的样式。

【问题讨论】:

  • 不是一个答案,但如果你也在 XAML 中定义新样式,它会比在后面的代码中更容易编写和阅读。
  • 我尝试先在 XAML 中定义样式,然后像这样设置 btn_bfx.Style = FindResource("RoundCornerBackground") as Style;它也没有工作。
  • 这可以在样式中使用数据触发器在没有代码的情况下完成。如果您将数据触发器绑定到表示您的条件更改的属性,则使用触发器中的设置器进行更改。

标签: wpf xaml resourcedictionary


【解决方案1】:

您的样式重写了按钮的ControlTemplate,因此不再使用背景颜色。

在 ControlTemplate 中,像这样向 Border 添加一个 Background 属性

<Border CornerRadius="0,0,4,4" BorderBrush="black" BorderThickness="1,0,1,1" 
    Background="{TemplateBinding Background}">

然后它会拾取你设置的任何背景。

【讨论】:

    【解决方案2】:

    这可以在样式中使用数据触发器在没有代码的情况下完成。如果将数据触发器绑定到表示条件更改的属性,则使用触发器中的设置器进行更改。例如:

    <Button>
                        <Button.Style>
                            <Style TargetType="Button">
                                <Setter Property="Background" Value="White"></Setter>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding condition}" Value="True">
                                        <Setter Property="Background" Value="Black"></Setter>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
                    </Button>
    

    这对我来说比您尝试调整样式的方式更容易、更整洁。它将所有内容保存在一个位置,而不是一半在 XAML 中,一半在后面的代码中。

    【讨论】:

    • 嗨。我没有使用 MVVM,因为它只是一个简单的 1 窗口项目,它从用户那里获取一些数据并通过 WEB API 更新一些表。我的意图是通过更改背景颜色,用户将获得他选择的选项的视觉反馈。所以点击按钮我试图改变背景颜色。就是这样。
    猜你喜欢
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 2018-03-22
    • 1970-01-01
    • 2018-11-25
    • 2014-09-03
    相关资源
    最近更新 更多