我需要这样做以便为自定义控件(继承 ContentControl)创建动态背景画笔。我的自定义控件有两个依赖属性:StartColor 和 EndColor。在自定义控件的控件模板中,控件被包裹在背景为RadialGradientBrush
带有渐变色标的边框中。一个渐变点的颜色绑定到 StartColor,另一个绑定到 EndColor。我在 XAML 中有这个 WORKING,但我需要将它转换为 VB 代码。XAML 中控件模板的边框元素使用以下代码完成:
<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>
我尝试在VB代码中创建边框如下,但它不起作用:
...
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 代码中实现这一点的任何想法?