사용자 지정 컨트롤에 대한 동적 배경 브러시를 만들려면이 작업을 수행해야합니다 (ContentControl 상속). 내 사용자 지정 컨트롤에는 StartColor 및 EndColor의 두 가지 종속성 속성이 있습니다. 사용자 지정 컨트롤의 컨트롤 템플릿에서 컨트롤은 그라디언트 멈춤이있는 RadialGradientBrush
인 배경 테두리에 래핑됩니다. 하나의 그래디언트 스톱의 색은 StartColor에 바인딩되고 다른 하나는 EndColor에 바인딩됩니다. XAML에서이 작업이 있지만 VB 코드로 변환해야합니다. XAML에서 컨트롤 템플릿의 경계 요소는 다음과 같은 코드로 이루어집니다 : 나는 다음과 같이 VB 코드의 테두리를 만들려고WPF : vb 코드에서 controlTemplate에있는 그라디언트 정지의 color 속성을 바인딩하려면 어떻게해야합니까?
<Style x:Key="{x:Type wpf:MyControl}"
TargetType="{x:Type wpf:MyControl}"
BasedOn="{StaticResource {x:Type ContentControl}}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type wpf:MyControl}">
...
<Border HorizontalAlignment="Stretch"
x:Name="background" Width="Auto"
Grid.RowSpan="3"
Opacity="0.9"
CornerRadius="{TemplateBinding CornerRadius}">
<Border.Background>
<Custom:RadialGradientBrush>
<Custom:GradientStop Color="{Binding Path=EndColor,
RelativeSource={RelativeSource TemplatedParent},
Mode=OneWay}"
Offset="0.462"/>
<Custom:GradientStop Color="{Binding StartColor,
RelativeSource={RelativeSource TemplatedParent},
Mode=OneWay}"
Offset="1"/>
</Custom:RadialGradientBrush>
</Border.Background>
</Border>
...
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
, 그러나 그것은 작동하지 않았다 :
...
Dim backgroundBorder As New FrameworkElementFactory(GetType(Border))
With backgroundBorder
.Name = "background"
.SetValue(Grid.RowSpanProperty, 3)
.SetValue(Grid.OpacityProperty, 0.9)
.SetBinding(Border.CornerRadiusProperty, New Binding("CornerRadius") With {.RelativeSource = New RelativeSource(RelativeSourceMode.TemplatedParent)})
End With
Dim backgroundBrush As New RadialGradientBrush()
Dim startColorGradientStop As New GradientStop()
startColorGradientStop.Offset = 1.0
BindingOperations.SetBinding(startColorGradientStop, GradientStop.ColorProperty, New Binding("StartColor") With {.RelativeSource = New RelativeSource(RelativeSourceMode.TemplatedParent), .Mode = BindingMode.OneWay})
backgroundBrush.GradientStops.Add(startColorGradientStop)
Dim endColorGradientStop As New GradientStop()
endColorGradientStop.Offset = 0.462
BindingOperations.SetBinding(endColorGradientStop, GradientStop.ColorProperty, New Binding("EndColor") With {.RelativeSource = New RelativeSource(RelativeSourceMode.TemplatedParent), .Mode = BindingMode.OneWay})
backgroundBrush.GradientStops.Add(endColorGradientStop)
backgroundBorder.SetValue(Border.BackgroundProperty, backgroundBrush)
...
VB 코드에서이 작업을 수행하는 방법에 대한 아이디어가 있습니까?
처음에는 xaml에 현재 프로젝트 네임 스페이스를 포함하고 있었기 때문에이 어셈블리가 어떤 어셈블리인지 지정하지 않았기 때문에 작동하지 않았습니다. 일단이 코드를 변경하면 모든 것이 잘되었습니다. 이전 방법보다 훨씬 쉽습니다. – Robin