所以,我最近有一個挑戰感到沮喪複製這種效果:XAML中的簡單懸停效果?
<style>
a:hover {background-color:yellow; }
</style>
使用WinRT中的XAML實現。
什麼是最簡潔的解決方案?
所以,我最近有一個挑戰感到沮喪複製這種效果:XAML中的簡單懸停效果?
<style>
a:hover {background-color:yellow; }
</style>
使用WinRT中的XAML實現。
什麼是最簡潔的解決方案?
好了,這裏是我的嘗試:
<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);
}
但是,當然有更好的方法!
我想你必須看看視覺狀態和VisualStateManager。我認爲Button控件是唯一具有視覺狀態的控件,這意味着您的視覺實體必須被定義爲Button(儘管它不必作爲一個按鈕)。在那篇文章的底部,你會找到一個例子。
This SO-question也可能有幫助,描述如何從現有的按鈕提取控制模板。這會給你一個起點,你可以根據你的需要進行修改。
至於「最濃縮的解決方案」,我也想看看。我見過的例子都需要20+行的XAML代碼,這對我不會覺得很凝結......
不,我不認爲Button是VisualStates唯一的控件。如果你拉起網格模板,他們使用主網格上的VisualStateManager來控制FullScreenLandscape,Snapped,Portrait等之間的轉換。 –
<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」。
在研究滾動查看器上的懸停情況時跨越此範圍。有趣的解決方案。 :-) – OmegaMan