wpf自定义控件像TemplateBinding一样发送画笔

发布于 2025-01-16 14:05:48 字数 2969 浏览 2 评论 0原文

您好,我为按钮创建了一个自定义控件,但发送画笔时出现问题 如果你在下面看到我在半径及其工作之前创建的,但画笔不是。 注意:如果我放一张图片来显示错误,而不是直接放代码

在此处输入图像描述

此处来源:

在此处输入图像描述

此处新代码:

public class WPFButton : ButtonBase
    {
        public CornerRadius Radius
        {
            get { return (CornerRadius)GetValue(RadiusProperty); }
            set { SetValue(RadiusProperty, value); }
        }

        public static readonly DependencyProperty RadiusProperty =
            DependencyProperty.Register("Radius", typeof(CornerRadius), typeof(WPFButton), new PropertyMetadata(new CornerRadius(0)));

        public Brush MouseHoverColor
        {
            get { return (Brush)GetValue(MouseHoverColorProperty); }
            set { SetValue(MouseHoverColorProperty, value); }
        }

        public static readonly DependencyProperty MouseHoverColorProperty =
            DependencyProperty.Register("MouseHoverColor", typeof(Brush), typeof(WPFButton), new PropertyMetadata(new SolidColorBrush()) );

        static WPFButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WPFButton), new FrameworkPropertyMetadata(typeof(WPFButton)));
        }
    }

Xaml

<Style TargetType="{x:Type local:WPFButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:WPFButton}">
                    <Border Name="Background" Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding Radius}">
                        <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" TargetName="Background" Value="{TemplateBinding MouseHoverColor}">

                            </Setter>
                        </Trigger>
                       
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" TargetName="Background" Value="#FF4792DC">

                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

hi i created a custom control for button but there is problem in send Brush
if you see down below i created before Radius and its work but the Brush is not.
Note: It would be better if I put a picture to show the errors, rather than putting the code directly

enter image description here

and here the source :

enter image description here

here new code :

public class WPFButton : ButtonBase
    {
        public CornerRadius Radius
        {
            get { return (CornerRadius)GetValue(RadiusProperty); }
            set { SetValue(RadiusProperty, value); }
        }

        public static readonly DependencyProperty RadiusProperty =
            DependencyProperty.Register("Radius", typeof(CornerRadius), typeof(WPFButton), new PropertyMetadata(new CornerRadius(0)));

        public Brush MouseHoverColor
        {
            get { return (Brush)GetValue(MouseHoverColorProperty); }
            set { SetValue(MouseHoverColorProperty, value); }
        }

        public static readonly DependencyProperty MouseHoverColorProperty =
            DependencyProperty.Register("MouseHoverColor", typeof(Brush), typeof(WPFButton), new PropertyMetadata(new SolidColorBrush()) );

        static WPFButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WPFButton), new FrameworkPropertyMetadata(typeof(WPFButton)));
        }
    }

Xaml

<Style TargetType="{x:Type local:WPFButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:WPFButton}">
                    <Border Name="Background" Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding Radius}">
                        <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" TargetName="Background" Value="{TemplateBinding MouseHoverColor}">

                            </Setter>
                        </Trigger>
                       
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" TargetName="Background" Value="#FF4792DC">

                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人爱我别走 2025-01-23 14:05:48

不要在 Setter 中使用 {TemplateBinding} 语法。这应该有效:

<Trigger Property="IsMouseOver" Value="True">
    <Setter Property="Background" TargetName="Background"
            Value="{Binding RelativeSource={RelativeSource TemplatedParent},
                Path=MouseHoverColor}">

    </Setter>
</Trigger>

Do not use the {TemplateBinding} syntax in a Setter. This should work:

<Trigger Property="IsMouseOver" Value="True">
    <Setter Property="Background" TargetName="Background"
            Value="{Binding RelativeSource={RelativeSource TemplatedParent},
                Path=MouseHoverColor}">

    </Setter>
</Trigger>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文