2012-11-08 38 views
0

嗯,我是WPF的新手,这是我第一次尝试改变WPF控件的样式。感谢Expression Blend,一切都比预期的更好,直到我做出这种风格。ColorAnimationUsingKeyFrames改变画笔

<Style TargetType="{x:Type TextBox}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TextBox}"> 
       <Grid> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"/> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PART_ContentHost"> 
             <EasingDoubleKeyFrame KeyTime="0" Value="0.5"/> 
            </DoubleAnimationUsingKeyFrames> 
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Background"> 
             <EasingColorKeyFrame KeyTime="0" Value="Red"/> 
            </ColorAnimationUsingKeyFrames> 
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="Background"> 
             <EasingColorKeyFrame KeyTime="0" Value="Yellow"/> 
            </ColorAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="ReadOnly"/> 
          <VisualState x:Name="MouseOver"/> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Rectangle x:Name="Background" Fill="{StaticResource ControlBackgroundBrush}" Stroke="{StaticResource NormalBrush}" RadiusX="2" RadiusY="2"/> 
        <ScrollViewer x:Name="PART_ContentHost" Margin="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" FontFamily="{TemplateBinding FontFamily}"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Foreground" Value="Black"/> 
    <Setter Property="SnapsToDevicePixels" Value="True"/> 
</Style> 

这两个刷在这里:

<Color x:Key="MainColor">#FF595959</Color> 
<Color x:Key="ControlBackgroundColor">#FF333333</Color> 

<SolidColorBrush x:Key="NormalBrush" Color="{DynamicResource MainColor}"/> 
<SolidColorBrush x:Key="ControlBackgroundBrush" Color="{StaticResource ControlBackgroundColor}" /> 

那么,什么是问题。禁用文本框应该改变文本框的边框颜色和背景,但它也会改变所有使用的颜色NormalBrush。我想只有几个通用的控件让主题容易修改。还有一件事我使用他们Usualy在其他样式作为StaticResource。您的建议和帮助将受到高度赞赏。

+0

正在发生,因为你是动画(逐步改变静态或共享资源)致毒。如果您只需要影响本地元素,则需要创建本地模板。对于你想要完成的任务,我认为'VisualState'方法是过分的,为了记录。 “ControlTemplate.Trigger”可以轻松触发本地彩色动画。 – XamlZealot

+0

另一种可能的方法(尽管我不确定这会对其他应用程序组件产生什么样的其他影响)是使画笔不共享: '' – XamlZealot

回答

1

StaticResource由静态(共享)定义,因此动画化STATIC资源的属性将影响在其范围内使用StaticResource的所有元素。通过标记X:共享的属性设置为false,WPF会为使用它与一个静态资源的每个元素创建一个实例:

<SolidColorBrush x:Key="OniiNormalBrush" x:Shared="False" Color="{StaticResource MainColor}"/> 
+1

而不是只发布一段代码,请*解释*为什么这段代码解决了问题。没有解释,这不是一个答案。 –

+0

只需编辑您的帖子以包含该信息。 :-) –

0

好吧,我从来没有发现,为什么一个颜色处处改变,第二个是不。正如在评论中提出的,我改变了这种风格,以使用触发器,而不是visualStates,一切工作正常。

<Style TargetType="{x:Type TextBox}"> 
<Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="{x:Type TextBox}"> 
      <Grid> 
       <Rectangle x:Name="Background" Fill="{StaticResource ControlBackgroundBrush}" Stroke="{StaticResource NormalBrush}" RadiusX="2" RadiusY="2"/> 
       <ScrollViewer x:Name="PART_ContentHost" Margin="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" FontFamily="{TemplateBinding FontFamily}"/> 
      </Grid> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="Fill" TargetName="Background" Value="Red" /> 
        <Setter Property="Stroke" TargetName="Background" Value="Green" /> 
        <Setter Property="Foreground" Value="Blue" /> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
</Setter.Value> 
</Setter> 
<Setter Property="Foreground" Value="Black"/> 
<Setter Property="SnapsToDevicePixels" Value="True"/> 

编辑

一切都通过NormalBrush定义,其中颜色是DynamicResource