所以,我打算做的是将我的单选按钮和选项卡项目模板化,使其看起来相同,以便它们看起来相同但行为不同。SoIi 创建了一个 Control 模板并将其定位到 Content Control 类型,并将其用作静态资源来模板化这两种类型。
一切正常,除了我无法弄清楚如何将标题设置为显示为标签。“内容”属性获取单选按钮标签文本,但是当我尝试绑定到“标题”属性时,我无法获取标签文本。
这是内容控件控制模板 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>