首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义ControlTemplate显示单词"ControlTemplate“

自定义ControlTemplate显示单词"ControlTemplate“
EN

Stack Overflow用户
提问于 2016-05-16 14:53:25
回答 1查看 57关注 0票数 1

我正在尝试修改ControlTemplate of CheckBox。我从ExpressionBlend中获得了默认模板,并将其放入CheckBox中。

为什么现在它会在System.Windows.Controls.ControlTemplate旁边显示文本“CheckBox”?

代码语言:javascript
复制
     <CheckBox IsChecked="True">
<!-- the following is from ExpressionBlend -->
                <ControlTemplate TargetType="{x:Type CheckBox}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="HoverOn">
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="BackgroundOverlay" Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="HoverOff">
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="BackgroundOverlay" Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="PressedOn">
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="PressedRectangle" Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="PressedOff">
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="PressedRectangle" Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="CheckedTrue">
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CheckIcon" Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" />
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="IndeterminateIcon" Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="CheckedFalse">
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CheckIcon" Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="IndeterminateIcon" Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="IndeterminateOn">
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CheckIcon" Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="IndeterminateIcon" Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="1" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="IndeterminateOff">
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="IndeterminateIcon" Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="FocussedOn">
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ContentFocusVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1"/>
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="FocussedOff">
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ContentFocusVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <BulletDecorator Background="Transparent" x:Name="bulletDecorator">
                        <BulletDecorator.Bullet>
                            <Grid Width="16" Height="16">
                                <Rectangle Height="14" Margin="1" x:Name="Background" 
                                   Width="14" Fill="{TemplateBinding Background}" 
                                   Stroke="{TemplateBinding BorderBrush}" 
                                   StrokeThickness="{TemplateBinding BorderThickness}" />
                                <Rectangle Height="14" Margin="1" x:Name="BackgroundOverlay" 
                                   Width="14" Opacity="0" Fill="{x:Null}" 
                                   Stroke="{StaticResource HoverBrush}" 
                                   StrokeThickness="2" />
                                <Rectangle Height="16" x:Name="PressedRectangle" 
                                   Width="16" IsHitTestVisible="false" Opacity="0" 
                                   Stroke="{StaticResource HoverBrush}" 
                                   StrokeThickness="1" />
                                <Path Height="10" Margin="1,1,0,1.5" x:Name="CheckIcon" 
                              Width="10.5" Opacity="0" Fill="{StaticResource GlyphBrush}" 
                              Stretch="Fill" 
                              Data="M102.03442,598.79645 L105.22962,597.78918 L106.78825,600.42358 C106.78825,600.42358 108.51028,595.74304 110.21724,593.60419 C112.00967,591.35822 114.89314,591.42316 114.89314,591.42316 C114.89314,591.42316 112.67844,593.42645 111.93174,594.44464 C110.7449,596.06293 107.15683,604.13837 107.15683,604.13837 z" />
                                <Rectangle Height="8" x:Name="IndeterminateIcon" 
                                   Width="10" Opacity="0" Fill="{StaticResource GlyphBrush}" />
                                <Rectangle Height="14" x:Name="DisabledVisualElement" 
                                   Width="14" Opacity="0" Fill="{StaticResource DisabledBackgroundBrush}" />
                                <Rectangle Height="16" x:Name="ContentFocusVisualElement" 
                                   Width="16" IsHitTestVisible="false" Opacity="0" 
                                   Stroke="{StaticResource HoverShineBrush}" 
                                   StrokeThickness="1" />
                                <Rectangle Height="12" x:Name="ShineRectangle" 
                                   Width="14" IsHitTestVisible="false" Opacity="1" 
                                   Stroke="{x:Null}" 
                                   StrokeThickness="1" 
                                   Fill="{StaticResource ShineBrush}" 
                                   Margin="2,2,2,2" />
                            </Grid>
                        </BulletDecorator.Bullet>
                        <ContentPresenter RecognizesAccessKey="True" 
                                  Margin="5,0,0,0" 
                                  VerticalAlignment="Top" HorizontalAlignment="Left" />
                    </BulletDecorator>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True">
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource FocussedOff}" 
                                         x:Name="FocussedOff_BeginStoryboard"/>
                            </Trigger.ExitActions>
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource FocussedOn}" 
                                         x:Name="FocussedOn_BeginStoryboard"/>
                            </Trigger.EnterActions>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsChecked" Value="True" />
                                <Condition Property="IsThreeState" Value="True" />
                            </MultiTrigger.Conditions>
                            <MultiTrigger.EnterActions>
                                <BeginStoryboard x:Name="ThreeStateOn_BeginStoryboard" Storyboard="{StaticResource CheckedFalse}" />
                            </MultiTrigger.EnterActions>
                            <MultiTrigger.ExitActions>
                                <BeginStoryboard x:Name="ThreeStateOff_BeginStoryboard" Storyboard="{StaticResource CheckedTrue}" />
                            </MultiTrigger.ExitActions>
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsChecked" Value="{x:Null}" />
                                <Condition Property="IsThreeState" Value="True" />
                            </MultiTrigger.Conditions>
                            <MultiTrigger.EnterActions>
                                <BeginStoryboard x:Name="ThreeStateOn1_BeginStoryboard" Storyboard="{StaticResource IndeterminateOn}" />
                            </MultiTrigger.EnterActions>
                            <MultiTrigger.ExitActions>
                                <BeginStoryboard x:Name="ThreeStateOff1_BeginStoryboard" Storyboard="{StaticResource IndeterminateOff}" />
                            </MultiTrigger.ExitActions>
                            <Setter Property="Visibility" TargetName="CheckIcon" Value="Hidden"/>
                        </MultiTrigger>
                        <Trigger Property="IsChecked" Value="True">
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource CheckedFalse}" 
                                         x:Name="CheckedTrue_BeginStoryboard"/>
                            </Trigger.ExitActions>
                            <Trigger.EnterActions>
                                <BeginStoryboard x:Name="CheckedTrue_BeginStoryboard1" Storyboard="{StaticResource CheckedTrue}"/>
                            </Trigger.EnterActions>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource HoverOn}"/>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource HoverOff}"/>
                            </Trigger.ExitActions>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Fill" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Background" />
                            <Setter Property="Stroke" Value="{DynamicResource DisabledBorderBrush}" TargetName="Background" />
                            <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}" />
                            <Setter Property="Opacity" TargetName="bulletDecorator" Value="0.5"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>


            </CheckBox>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-16 15:05:47

这是因为您将ControlTemplate赋值为CheckBox的值。应该将其分配给Template属性:

代码语言:javascript
复制
<CheckBox IsChecked="True">
    <CheckBox.Template>
        <ControlTemplate TargetType="{x:Type CheckBox}">
            ...
        </ControlTemplate>
    </CheckBox.Template>
</CheckBox>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37256863

复制
相关文章

相似问题

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