【问题标题】:How to change control in style programmatically如何以编程方式更改样式控制
【发布时间】:2019-04-11 22:18:38
【问题描述】:

我有一个按钮样式。我无法更改边框的 CornerRadius,模板中有什么。

风格:

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

【问题讨论】:

    标签: c# wpf templates styles controltemplate


    【解决方案1】:

    你可以这样使用你的风格:

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

    创建一个新的类,如:

    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>
    

    更新:

    或者您可以通过编程方式更改边框的 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;
                }
            }
        }
    
    }
    

    【讨论】:

    • 谢谢!它对我有帮助。
    • 好的,如果回答了你的问题,你能接受吗?
    猜你喜欢
    • 1970-01-01
    • 2010-09-11
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多