簡體   English   中英

wpf 自定義控件發送刷像TemplateBinding

[英]wpf custom control send brush like TemplateBinding

嗨,我為按鈕創建了一個自定義控件,但是如果您在下面看到我在 Radius 及其工作之前創建的但刷子不是,則發送刷子時會出現問題。 注意:如果我放一張圖片來顯示錯誤會更好,而不是直接放代碼

在此處輸入圖像描述

這里的來源:

在此處輸入圖像描述

這里是新代碼:

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>

不要在Setter中使用{TemplateBinding}語法。 這應該工作:

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

    </Setter>
</Trigger>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM