根据继承属性设置依赖属性的默认值

时间:2022-10-05 18:38:08

I'm creating a custom control library and I started with a simple round edge separator but there is something I can't seem to be able to do.

我创建了一个自定义控制库,我开始使用一个简单的圆形边缘分隔符,但是有些东西我似乎无法做到。

I have a CornerRadius dependency property and I would like CornerRadius to be equal to Height / 2 if CornerRadius is not defined or take the user value otherwise. I initialize the height in the constructor so that it's never null

我有一个拐角半径依赖属性,我希望拐角半径等于高度/ 2,如果拐角半径没有定义,或者取用户值。我在构造函数中初始化高度,使它永不为空

I know how to define a default value for the dependency property, but I don't know how, or if it is possible to set the CornerRadius value according to the Height of the control. I've searched a while in vain so far.

我知道如何为dependency属性定义一个默认值,但是我不知道如何定义,或者是否可以根据控件的高度设置CornerRadius值。到目前为止,我找了半天也没有找到。

Xaml file

Xaml文件

<!-- Rounded Separator -->
<Style TargetType="{x:Type local:RoundedSeparator}" BasedOn="{StaticResource {x:Type Separator}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:RoundedSeparator}">

                <!-- Mask for round edges -->
                <Grid Height="{TemplateBinding Height}"
                      Width="{TemplateBinding Width}">
                    <Border Name="PART_TitleBarMask" 
                            CornerRadius="{TemplateBinding CornerRadius}"
                            Background="White"/>

                    <Grid>
                        <Grid.OpacityMask>
                            <VisualBrush Visual="{Binding ElementName=PART_TitleBarMask}"/>
                        </Grid.OpacityMask>

                        <!-- Separator -->
                        <Rectangle Fill="{TemplateBinding Background}"/>
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

.cs file

cs文件

public class RoundedSeparator : Separator
{
    static RoundedSeparator()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(RoundedSeparator), new FrameworkPropertyMetadata(typeof(RoundedSeparator)));
    }

    public RoundedSeparator()
    {
        this.Height = 100;
        this.Background = Brushes.Black;
    }

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

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

1 个解决方案

#1


0  

try :

试一试:

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

      RoundedSeparator.HeightProperty.OverrideMetadata
               (typeof(RoundedSeparator), new FrameworkPropertyMetadata(new PropertyChangedCallback(HeightPropertyChanged)));
  }


  private static void HeightPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
         // Do what you will .... :)
  }

#1


0  

try :

试一试:

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

      RoundedSeparator.HeightProperty.OverrideMetadata
               (typeof(RoundedSeparator), new FrameworkPropertyMetadata(new PropertyChangedCallback(HeightPropertyChanged)));
  }


  private static void HeightPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
         // Do what you will .... :)
  }