在 ControlTemplate 中使用 {TemplateBinding Content} withTextBlock

发布于 2024-08-09 19:29:47 字数 4965 浏览 7 评论 0原文

基本上我有一个按钮,我为其构建了一个控件模板。它目前正在工作,但 VS 2010 抱怨我的控件模板中的以下代码行

<TextBlock Text="{TemplateBinding Content}" 
    Foreground="{TemplateBinding Foreground}" 
    x:Name="cContentTextBlock"  />

该控件模板用于按钮,并且我有多种针对此 TextBlock 的 VisualState。

我明白为什么 VS2010 会抱怨...如果内容实际上不是文本怎么办?这会引起问题。对我来说,最重要的是我想设置文本的前景以响应视觉状态的变化。

关于我如何实现这一目标有什么想法吗? (试试吧,确实有效...但是vs2010的设计师噎住了)

以下是整个风格:

<Style x:Key="PassiveLinkButton" TargetType="Button">
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid>
                    <vsm:VisualStateManager.VisualStateGroups>
                        <vsm:VisualStateGroup x:Name="CommonStates">
                            <vsm:VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="cMouseOverBorder" Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <SolidColorBrush Color="Black" />
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                            <vsm:VisualState x:Name="Normal"/>
                            <vsm:VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="cMouseOverBorder" Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <SolidColorBrush Color="Blue" />
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="cContentTextBlock" Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <SolidColorBrush Color="Blue" />
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                            <vsm:VisualState x:Name="Disabled"/>
                        </vsm:VisualStateGroup>
                    </vsm:VisualStateManager.VisualStateGroups>
                    <Border x:Name="cFocusBorder" 
                                BorderThickness="1" 
                                BorderBrush="{StaticResource BorderBrush}"
                                Margin="2">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Border x:Name="cMouseOverBorder" 
                                BorderBrush="Transparent" BorderThickness="0,0,0,1" Margin="0 0 0 2">
                                <StackPanel HorizontalAlignment="Left">
                                    <TextBlock Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" 
                                                x:Name="cContentTextBlock" Margin="2 2 2 0"
                                                HorizontalAlignment="Center" />
                                </StackPanel>
                            </Border>
                        </Grid>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>

    </Setter>
</Style>

可以这样使用:

<Button Content="Press Me" Style={StaticResource PassiveButtonLink}" />

Basically I have a button that I built a control template for. It is currently working but VS 2010 complains about the following line of code in my control template

<TextBlock Text="{TemplateBinding Content}" 
    Foreground="{TemplateBinding Foreground}" 
    x:Name="cContentTextBlock"  />

The control template is for a Button and I have a variety of VisualStates that target this TextBlock.

I can see why VS2010 complains... What if the content isn't actually text? This would cause problems. For me the big deal is that I want to set the Foreground of the text in response to Visual State changes.

Any ideas on how I could accomplish this? (try it, it works... but the vs2010 designer chokes on it)

The following is the entire style:

<Style x:Key="PassiveLinkButton" TargetType="Button">
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid>
                    <vsm:VisualStateManager.VisualStateGroups>
                        <vsm:VisualStateGroup x:Name="CommonStates">
                            <vsm:VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="cMouseOverBorder" Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <SolidColorBrush Color="Black" />
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                            <vsm:VisualState x:Name="Normal"/>
                            <vsm:VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="cMouseOverBorder" Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <SolidColorBrush Color="Blue" />
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="cContentTextBlock" Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <SolidColorBrush Color="Blue" />
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                            <vsm:VisualState x:Name="Disabled"/>
                        </vsm:VisualStateGroup>
                    </vsm:VisualStateManager.VisualStateGroups>
                    <Border x:Name="cFocusBorder" 
                                BorderThickness="1" 
                                BorderBrush="{StaticResource BorderBrush}"
                                Margin="2">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Border x:Name="cMouseOverBorder" 
                                BorderBrush="Transparent" BorderThickness="0,0,0,1" Margin="0 0 0 2">
                                <StackPanel HorizontalAlignment="Left">
                                    <TextBlock Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" 
                                                x:Name="cContentTextBlock" Margin="2 2 2 0"
                                                HorizontalAlignment="Center" />
                                </StackPanel>
                            </Border>
                        </Grid>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>

    </Setter>
</Style>

Which would be used like:

<Button Content="Press Me" Style={StaticResource PassiveButtonLink}" />

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

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

发布评论

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

评论(2

白色秋天 2024-08-16 19:29:47

如果您还没有阅读此 MSDN 文档,我会阅读该文档:

演练:使用 ControlTemplate 自定义按钮的外观
http://msdn.microsoft.com/en-us /library/cc903963(VS.95).aspx

如果您需要更深入地了解:

通过使用 ControlTemplate 自定义现有控件的外观
http://msdn.microsoft.com/en-us /library/cc189093(VS.95).aspx

通过创建 ControlTemplate 创建新控件
http://msdn.microsoft.com/en-us /library/cc278064(VS.95).aspx

I would read through this MSDN documentation if you haven't already:

Walkthrough: Customizing the Appearance of a Button by Using a ControlTemplate
http://msdn.microsoft.com/en-us/library/cc903963(VS.95).aspx

and if you need to go deeper than that:

Customizing the Appearance of an Existing Control by Using a ControlTemplate
http://msdn.microsoft.com/en-us/library/cc189093(VS.95).aspx

Creating a New Control by Creating a ControlTemplate
http://msdn.microsoft.com/en-us/library/cc278064(VS.95).aspx

捶死心动 2024-08-16 19:29:47

我有类似的问题。我的解决方案是更改我的默认 XML 命名空间。

以下内容在我的 VS2008 副本中有效,但在 VS2010 中无效

xmlns="http://schemas.microsoft.com/netfx/2007/xaml/presentation"

以下内容在 VS2008 和 VS2010 中均有效:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

通过“有效”,我的意思是 WPF 设计者认识到模板绑定元素。

I had a similar problem. The solution for me was to change my default XML namespace.

The following works in my copy of VS2008, but not in VS2010:

xmlns="http://schemas.microsoft.com/netfx/2007/xaml/presentation"

The following works in both VS2008 and VS2010:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

By "works", I mean that the WPF designer recognizes the TemplateBinding element.

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