首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在vb代码中绑定位于controlTemplate中的gradientstop的颜色属性?

如何在vb代码中绑定位于controlTemplate中的gradientstop的颜色属性?
EN

Stack Overflow用户
提问于 2009-08-05 16:57:27
回答 2查看 2.1K关注 0票数 1

为了创建自定义控件的动态背景画笔(继承ContentControl),我需要这样做。我的自定义控件有两个依赖属性: StartColor和EndColor。在自定义控件的控件模板中,控件被包装在一个边框中,该边框的背景是一个带有渐变停靠点的RadialGradientBrush。一个渐变色标的颜色绑定到StartColor,另一个渐变色标的颜色绑定到EndColor。我有这个工作在XAML,但我需要把它转换成VB代码。XAML中控件模板的边框元素由以下代码完成:

代码语言:javascript
复制
<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代码创建边框,如下所示,但它不起作用:

代码语言:javascript
复制
...
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代码实现这一点有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-08-05 20:55:11

根据微软的说法,你知道FrameworkElementFactory方法不再被推荐了吗?推荐的方法是使用XamlReader.Parse在代码中创建任何元素/资源。

票数 1
EN

Stack Overflow用户

发布于 2009-08-05 18:09:17

您必须访问边框应该是其内容的父ContentControl。并在你的VB代码中进行设置。

我在这里所说的ContentControl是指无论哪个控件是边框的父控件,您都需要在OnApplyTemplate覆盖函数上访问它,并将VB.NET创建的边框作为子控件添加到该可视化中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1234478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档