커스텀컨트롤 DependencyProperty 설정

xaml과 cs로 커스텀컨트롤을 제작하고 있습니다. 하지만 이제 시작하는 단계다보니 어려운 것이 많네요.
제 질문은 DropShadowEffect BlurRadius=“20” ShadowDepth=“13” Direction=“315” Color=“#33D1D9E6
여기 부분의 속성값들을 DependencyProperty로 설정하여 다른 xaml에서 속성값 설정하듯이 하는 방법이 어떻게 되는지 입니다.
상위의 Border에서는 templateBinding을 사용해서 설정값이 반영되는지 확인했으나 같은 방법으로 시도해보아도 적용되지 않네요…

다음은 저의 코드입니다.
GlassmorphismButton.xaml

<Style TargetType="{x:Type local:GlassmorphismButton}" BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="Background" Value="{StaticResource DefaultBackground}"/>
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="Width" Value="100"/>
    <Setter Property="Height" Value="100"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:GlassmorphismButton}">
                <Border x:Name="FirstBorder" Background="{TemplateBinding Background}"  CornerRadius="{TemplateBinding BorderCornerRadius}" >
                    <Border.Effect>
                        <DropShadowEffect BlurRadius="20" ShadowDepth="13" Direction="315" Color="#33D1D9E6"/>
                    </Border.Effect>

GlassmorphismButton.cs

 public class GlassmorphismButton : Button
 {
     public static readonly DependencyProperty BorderCornerRadiusProperty =
         DependencyProperty.Register(
             "BorderCornerRadius",
             typeof(CornerRadius),
             typeof(GlassmorphismButton),
             new PropertyMetadata());

     public CornerRadius BorderCornerRadius
     {
         get => (CornerRadius)GetValue(BorderCornerRadiusProperty);
         set => SetValue(BorderCornerRadiusProperty, value);
     }

     public static readonly DependencyProperty BlurRadiusProperty =
         DependencyProperty.Register(
             "BlurRadius",
             typeof(double),
             typeof(GlassmorphismButton),
             new PropertyMetadata());

     public double BlurRadius
     {
         get => (double)GetValue(BlurRadiusProperty);
         set => SetValue(BlurRadiusProperty, value);
     }


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


 }

Themes/Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:GlassmorphismControl.Unit">


    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/GlassmorphismControl.Unit;component/GlassmorphismButton.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

고수님들의 도움을 기다리겠습니다. 감사합니다!

1 Like
<Setter Property="Template">
	<Setter.Value>
		<ControlTemplate TargetType="{x:Type local:GlassmorphismButton}">
			<Border
				x:Name="FirstBorder"
				Background="{TemplateBinding Background}"
				CornerRadius="{TemplateBinding BorderCornerRadius}">
				<Border.Effect>
					<DropShadowEffect
						BlurRadius="{Binding BlurRadius, RelativeSource={RelativeSource TemplatedParent}}"
						Direction="315"
						ShadowDepth="13"
						Color="#33D1D9E6" />
				</Border.Effect>
			</Border>
		</ControlTemplate>
	</Setter.Value>
</Setter>

RelativeSource, TemplatedParent 통해 Binding 해보시는건 어떤가요 ?

3 Likes

우와… 감사합니다! 그냥 binding만 했을때는 되지 않았는데 RelativeSource도 해줘야 하는군요

2 Likes