模板内容控件有多种类型 - 单选按钮和tabitem

时间:2013-05-24 17:21:05

标签: wpf silverlight xaml contentcontrol control-template

所以,我打算做的是模板我的单选按钮和标签项看起来相同,以便它们看起来相同,但可以采取不同的行为。 SoIi创建了一个Control模板,并将其定位到Content Control类型,并将其用作静态资源来模拟这两种类型。

一切正常,除了我无法弄清楚如何设置标题显示为标签。 'Content'属性选择单选按钮标签文本,但是当我尝试绑定到'Header'属性时,我无法获取标签文本。

以下是内容控件Control Template Xaml:

 <!-- Tab Selector Style -->
<ControlTemplate x:Key="TabSelectorStyle" TargetType="ContentControl">
    <Grid x:Name="Root" HorizontalAlignment="Stretch">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates" >
                <VisualState x:Name="Normal" >
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Box" Storyboard.TargetProperty="Fill">
                            <ObjectAnimationUsingKeyFrames.KeyFrames>
                                <DiscreteObjectKeyFrame KeyTime="0:0:0.1">
                                    <DiscreteObjectKeyFrame.Value>
                                        <SolidColorBrush Color="#FF323232"/>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                            </ObjectAnimationUsingKeyFrames.KeyFrames>
                        </ObjectAnimationUsingKeyFrames>
                        <DoubleAnimation Storyboard.TargetName="Box" Storyboard.TargetProperty="StrokeThickness" To="1" Duration="0:0:0.1" />
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="MouseOver">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Box" Storyboard.TargetProperty="Fill">
                            <ObjectAnimationUsingKeyFrames.KeyFrames>
                                <DiscreteObjectKeyFrame KeyTime="0:0:0.3">
                                    <DiscreteObjectKeyFrame.Value>
                                        <SolidColorBrush Color="#FF326699"/>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                            </ObjectAnimationUsingKeyFrames.KeyFrames>
                        </ObjectAnimationUsingKeyFrames>
                        <DoubleAnimation Storyboard.TargetName="Box" Storyboard.TargetProperty="StrokeThickness" To="1.5" Duration="0:0:0.3" />
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="SelectionStates" >
                <VisualState x:Name="Selected" >
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="Check" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.3" />
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Unselected" >
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="Check" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.3" />
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Margin="{Binding Path=Margin}">
            <Grid  Margin="5,2,5,2" HorizontalAlignment="Center">
                <Rectangle x:Name="Box"
                          Width="10" Height="10"
                          Fill="#FF323232" Stroke="#FFFFFFFF" StrokeThickness="1"
                          VerticalAlignment="Center" HorizontalAlignment="Center"/>
                <Rectangle x:Name="Check"
                         Width="8" Height="8"
                         Fill="#FFFFFFFF" Opacity="0"
                         VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
            <TextBlock x:Name="RadioButtionTitle" Foreground="#FFFFFFFF" Text="{TemplateBinding Content}" HorizontalAlignment="Stretch" Width="Auto" Margin="5,2,5,2"/>

            <!-- F I X E D   H E R E -->
            <TextBlock x:Name="TabitemTitle" Foreground="#FFFFFFFF" Text="{Binding Header, RelativeSource={RelativeSource FindAncestor, AncestorType=presentcontrols:TabItem}}" HorizontalAlignment="Stretch" Width="Auto" Margin="5,2,5,2"/>
            <!-- =================== -->
        </StackPanel>
    </Grid>
</ControlTemplate>

0 个答案:

没有答案