2012-03-14 55 views
1

我需要一个故事板,它可以在我的TextBox中改变前景。问题是这个TextBox必须在DataTemplate中。DataTemplate中的故事板不起作用

如何更改我的xaml以使其工作?

<DataTemplate x:Key="contentTexBox"> 
      <Grid> 
       <VisualStateManager.VisualStateGroups> 
        <VisualStateGroup x:Name="CommonStates"> 
         <VisualState x:Name="Normal"/> 
         <VisualState x:Name="MouseOver"> 
          <Storyboard> 
           <ColorAnimation Duration="0" To="Pink" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="tbContent"/> 
          </Storyboard> 
         </VisualState> 
        </VisualStateGroup> 
       </VisualStateManager.VisualStateGroups> 
       <TextBox Text="Test text" Width="200" Height="35" Foreground="Blue" x:Name="tbContent" BorderBrush="Purple">     

       </TextBox> 
      </Grid> 
     </DataTemplate> 
+0

对不起,我想我不明白的问题,因为你的作品样本。问题是什么? – Zabavsky 2012-03-14 17:30:01

+0

在鼠标悬停时,它是否将前景从粉红色更改为蓝色? – user278618 2012-03-16 11:28:10

+0

从蓝色到粉红色。 – Zabavsky 2012-03-16 12:09:28

回答

-1

我做了另一种方式,但这种方法没有明确提到文本框,所以我想它应该适用于你的情况。主要的变化是对TextBox风格的调用现在是隐式的(因为状态是在这个控制下声明的)并且Background属性被改变而不是Foreground

适应测试代码中,我写信给你的情况后,我想这可以看或多或少是这样的:

<DataTemplate x:Key="contentCheckBox"> 
<Grid> 
<TextBox Text="Test text" Width="200" Height="35" Foreground="Blue" BorderBrush="Purple"> 
     <TextBox.Style> 
      <Style TargetType="TextBox"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="TextBox"> 
          <TextBox Width="190"> 
           <VisualStateManager.VisualStateGroups> 
            <VisualStateGroup x:Name="CommonStates"> 
             <VisualState x:Name="Normal"/> 
             <VisualState x:Name="MouseOver"> 
              <Storyboard> 
               <ColorAnimation Duration="0" To="Pink" Storyboard.TargetProperty="Background.Color" /> 
              </Storyboard> 
             </VisualState> 
            </VisualStateGroup> 
           </VisualStateManager.VisualStateGroups> 
          </TextBox> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </TextBox.Style> 
    </TextBox> 
    </Grid> 
    </DataTemplate> 
+0

感谢@Lucas的帮助。我必须在这里设定一个Target/TargetName,但我不知道如何。你能编辑你的答案吗? – user278618 2012-03-16 11:41:48

+0

以及TargetType =“{x:Type TextBox}”应该是TargetType =“TextBox”。 x:类型在wpf – user278618 2012-03-16 11:42:50

+0

是的,对不起,我已经在WPF中用'x:Type'测试了这个。在谈论'TargetName'时,你有没有尝试过设置它(如代码片段所示)? 'Storyboard'被定义为'TextBox'的子元素,所以我想你不必明确地指定它。 – 2012-03-16 19:02:25