2

カスタム コントロール ライブラリを作成していて、単純なラウンド エッジ セパレーターから始めましたが、できないように見えることがあります。

CornerRadius 依存関係プロパティがあり、CornerRadius が定義されていない場合は、CornerRadius を Height / 2 に等しくするか、それ以外の場合はユーザー値を取得したいと考えています。コンストラクターで高さを初期化して、null にならないようにします

依存関係プロパティの既定値を定義する方法は知っていますが、コントロールの高さに応じて CornerRadius 値を設定する方法や可能かどうかはわかりません。私はこれまで無駄にしばらく検索してきました。

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 ファイル

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

1 に答える 1