基本上我有一个按钮,我为其构建了一个控制模板。它目前正在运行,但 VS 2010 在我的控件模板中抱怨以下代码行
<TextBlock Text="{TemplateBinding Content}"
Foreground="{TemplateBinding Foreground}"
x:Name="cContentTextBlock" />
控件模板用于按钮,我有多种针对此 TextBlock 的 VisualStates。
我明白为什么 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}" />