WPF 将 ControlTemplate 的内容绑定到控件中的属性?

发布于 2024-12-09 12:58:45 字数 1493 浏览 1 评论 0原文

我想将按钮 ControlTemplate 中的 Border.Background 绑定到按钮的背景属性。通常我会使用 TemplateBinding:

  <Style TargetType="Button" x:Key="ColuredButton">
                <Setter Property="Background" Value="LightGreen"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border x:Name="Border" CornerRadius="2" BorderThickness="1" BorderBrush="Gray">
                               <Border.Background>
                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                        <GradientStop Color="{TemplateBinding Foreground}"/>
                                        <GradientStop Color="{TemplateBinding Background}"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                                    <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" />
                            </Border> 
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

但我收到错误:“如果不在模板中,则无法设置 TemplateBinding”..但我在模板中! (如果我不使用 LinearGradientBrush 并将边框 Backround 属性直接绑定到 {TemplateBinding Background} ,它就可以工作......

I want to bind Border.Background in my Button ControlTemplate to the Background Property of my button. Usually I would use a TemplateBinding:

  <Style TargetType="Button" x:Key="ColuredButton">
                <Setter Property="Background" Value="LightGreen"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border x:Name="Border" CornerRadius="2" BorderThickness="1" BorderBrush="Gray">
                               <Border.Background>
                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                        <GradientStop Color="{TemplateBinding Foreground}"/>
                                        <GradientStop Color="{TemplateBinding Background}"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                                    <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" />
                            </Border> 
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

But I get the error: "Cannot set TemplateBinding if not in a Template".. But I am in a Template! (It works if I dont use LinearGradientBrush and bind the borders Backround Property directly to {TemplateBinding Background}....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2024-12-16 12:58:45

正如@Snowbear所说,您应该将Color绑定到Color,而不是将Color绑定到Brush。但在他的解决方案中,不允许具有深层属性 Path(例如 Foreground.Color)的 TemplateBinding 作为绑定标记的一部分。

所以使用以下...

    <Border.Background>
      <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
         <GradientStop Color="{Binding Foreground.Color,
                               RelativeSource={RelativeSource TemplatedParent}}"
                       Offset="0.2"/>
         <GradientStop Color="{Binding Background.Color,
                               RelativeSource={RelativeSource TemplatedParent}}"
                       Offset="0.6"/>
      </LinearGradientBrush>
   </Border.Background>

它应该可以工作。

As @Snowbear said, you should bind Color to Color and not Color to Brush. But in his solution, TemplateBinding with deep property Path such as Foreground.Color isnt allowed as part of the binding markup.

So use the following...

    <Border.Background>
      <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
         <GradientStop Color="{Binding Foreground.Color,
                               RelativeSource={RelativeSource TemplatedParent}}"
                       Offset="0.2"/>
         <GradientStop Color="{Binding Background.Color,
                               RelativeSource={RelativeSource TemplatedParent}}"
                       Offset="0.6"/>
      </LinearGradientBrush>
   </Border.Background>

And it should work.

云胡 2024-12-16 12:58:45

我认为你可能在这里有一些其他错误,但没有得到很好的报告。 GradientStop 接受其相应属性中的 Color,而 ButtonBackgroundForeground 属性> 是画笔,不是颜色。如果您认为 BackgroundForeground 将是 SolidColorBrush,您可以尝试在绑定中访问它们的 Color 属性,但是我不确定它是否会起作用:

<GradientStop Color="{TemplateBinding Foreground.Color}"/>

I think you might have some other error here, but it is not reported well. GradientStop accepts a Color in its corresponding property while Background and Foreground properties of a Button are brushes, not colors. If you think that Background and Foreground will be SolidColorBrush you might try accessing their Color property in your binding, but I'm not sure whether it will work or not:

<GradientStop Color="{TemplateBinding Foreground.Color}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文