2012-06-22 35 views

回答

2

好了,這裏是我的嘗試:

<VisualStateManager.VisualStateGroups> 
    <VisualStateGroup x:Name="VisualStateGroup"> 
     <VisualState x:Name="Normal"/> 
     <VisualState x:Name="Hover"> 
      <Storyboard> 
       <ColorAnimation To="Yellow" Duration="0" 
        Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" 
        Storyboard.TargetName="MyTextBox" /> 
      </Storyboard> 
     </VisualState> 
    </VisualStateGroup> 
</VisualStateManager.VisualStateGroups> 

<Grid x:Name="MyTextBox" Background="White" 
     PointerEntered="MyTextBox_PointerEntered" 
     PointerExited="MyTextBox_PointerExited" 
     Height="114" Width="537"> 
</Grid> 

這:

private void MyTextBox_PointerEntered(object sender, PointerRoutedEventArgs e) 
{ 
    VisualStateManager.GoToState(this, Hover.Name, false); 
} 

private void MyTextBox_PointerExited(object sender, PointerRoutedEventArgs e) 
{ 
    VisualStateManager.GoToState(this, Normal.Name, false); 
} 

但是,當然有更好的方法!

+0

在研究滾動查看器上的懸停情況時跨越此範圍。有趣的解決方案。 :-) – OmegaMan

0

我想你必須看看視覺狀態和VisualStateManager。我認爲Button控件是唯一具有視覺狀態的控件,這意味着您的視覺實體必須被定義爲Button(儘管它不必作爲一個按鈕)。在那篇文章的底部,你會找到一個例子。

This SO-question也可能有幫助,描述如何從現有的按鈕提取控制模板。這會給你一個起點,你可以根據你的需要進行修改。

至於「最濃縮的解決方案」,我也想看看。我見過的例子都需要20+行的XAML代碼,這對我不會覺得很凝結......

+1

不,我不認爲Button是VisualStates唯一的控件。如果你拉起網格模板,他們使用主網格上的VisualStateManager來控制FullScreenLandscape,Snapped,Portrait等之間的轉換。 –

2
<VisualStateManager.VisualStateGroups> 
      <VisualStateGroup x:Name="CommonStates"> 
       <VisualState x:Name="Normal"/> 
       <VisualState x:Name="PointerOver"> 
        <Storyboard> 
         <ColorAnimation To="Yellow" Duration="0" 
       Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" 
       Storyboard.TargetName="MyTextBox" /> 
        </Storyboard> 
       </VisualState> 
       <VisualState x:Name="Pressed"/> 
      </VisualStateGroup> 
</VisualStateManager.VisualStateGroups> 

RT中的「懸停」狀態是「PointerOver」。