2012-03-14 57 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