如何以编程方式更改样式的控件

时间:2018-11-08 11:52:44

标签: c# wpf templates styles controltemplate

我有一个按钮样式。我无法更改“ CornerRadius for Border”(模板中的内容)。

样式:

<Style TargetType="Button" x:Key="Circle">
        <Setter Property="Background" Value="#373737"/>
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="Bord1" CornerRadius="20" Background="{TemplateBinding Background}">
                        <Grid>
                            <TextBlock Name="tx" Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="{TemplateBinding FontSize}" Foreground="White"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#EBEBEB" />
                            <Setter TargetName="tx" Property="Foreground" Value="Black"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" >
                                <Setter.Value>
                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
                                        <GradientStop Color="#F3F3F3" Offset="0.35"/>
                                        <GradientStop Color="#FFC9C7BA" Offset="0.95"/>
                                        <GradientStop Color="#CDCDCD" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter TargetName="tx" Property="RenderTransform" >
                                <Setter.Value>
                                    <TranslateTransform Y="1.0" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我写了这段代码,但是对我没有帮助。

Style A = Application.Current.Resources["Circle"] as Style;
Setter A1 = (Setter)A.Setters[2];
ControlTemplate C1 = (ControlTemplate)A1.Value;
Border B = (Border)C1.LoadContent();
B.SetValue(Border.CornerRadiusProperty, new CornerRadius(2));

1 个答案:

答案 0 :(得分:0)

您可以使用如下样式:

将模板中的固定角半径更改为模板绑定

创建一个新的类,例如:

public class MyButton : Button {

    public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register(
        "CornerRadius", typeof(CornerRadius), typeof(MyButton), new PropertyMetadata(default(CornerRadius)));

    public CornerRadius CornerRadius {
        get { return (CornerRadius) GetValue(CornerRadiusProperty); }
        set { SetValue(CornerRadiusProperty, value); }
    }
}

在您的样式和xaml中使用这样的类:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="{Binding Path=Content.Title, ElementName=MainFrame}" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="local:MyButton" x:Key="Circle">
            <Setter Property="Background" Value="#373737"/>
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="local:MyButton">
                        <Border Name="Bord1" CornerRadius="{TemplateBinding CornerRadius}" Background="{TemplateBinding Background}">
                            <Grid>
                                <TextBlock Name="tx" Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="{TemplateBinding FontSize}" Foreground="White"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="#EBEBEB" />
                                <Setter TargetName="tx" Property="Foreground" Value="Black"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Background" >
                                    <Setter.Value>
                                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
                                            <GradientStop Color="#F3F3F3" Offset="0.35"/>
                                            <GradientStop Color="#FFC9C7BA" Offset="0.95"/>
                                            <GradientStop Color="#CDCDCD" Offset="1"/>
                                        </LinearGradientBrush>
                                    </Setter.Value>
                                </Setter>
                                <Setter TargetName="tx" Property="RenderTransform" >
                                    <Setter.Value>
                                        <TranslateTransform Y="1.0" />
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel>
        <local:MyButton Style="{StaticResource Circle}" CornerRadius="10"></local:MyButton>
    </StackPanel>
</Window>

更新:

或者您可以通过编程方式为Border更改CornerRadius,例如:

public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();
        Loaded += WasLoaded;
    }

    private void WasLoaded(object sender, RoutedEventArgs e) {

        var children = VisualTreeHelper.GetChildrenRecursive(Button1);
        foreach (var child in children.OfType<Border>()) {
            if (child.Name == "Bord1") {
                child.CornerRadius = new CornerRadius(1);
                break;
            }
        }

    }

}

public static class VisualTreeHelper {
    /// <summary>
    /// Enumerates through element's children in the visual tree.
    /// </summary>
    public static IEnumerable<DependencyObject> GetChildrenRecursive(this DependencyObject element) {
        if (element == null) {
            throw new ArgumentNullException("element");
        }

        for (var i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(element); i++) {
            var child = System.Windows.Media.VisualTreeHelper.GetChild(element, i);
            yield return child;

            foreach (var item in child.GetChildrenRecursive()) {
                yield return item;
            }
        }
    }

}