0

我有一个包含 StackPanel 的拆分视图,我想定义一个样式,当悬停在每个 StackPanel 中时修改颜色,这可能吗?

这是我的代码:

 <StackPanel Orientation="Horizontal" Style="{Binding Source={StaticResource HyperlinkPointerOverForegroundThemeBrush}}" >
                    <Button x:Name="MenuButton1"
                    Width="50" Height="50" Background="Transparent">
                        <Button.Content>
                            <Image Source="images/chercher.png"></Image>
                        </Button.Content>
                    </Button>
                    <TextBlock Text="Resultats" FontSize="18" VerticalAlignment="Center" Foreground="#727271"/>
                </StackPanel>

我已经定义了这样的样式:

 <SolidColorBrush x:Key="HyperlinkButtonBackgroundThemeBrush" Color="#e6e6e6" />
        <SolidColorBrush x:Key="HyperlinkButtonBorderThemeBrush" Color="#e6e6e6" />
        <SolidColorBrush x:Key="HyperlinkDisabledThemeBrush" Color="#e6e6e6" />
        <SolidColorBrush x:Key="HyperlinkForegroundThemeBrush" Color="#e6e6e6" />
        <SolidColorBrush x:Key="HyperlinkPointerOverForegroundThemeBrush" Color="#e6e6e6" />
        <SolidColorBrush x:Key="HyperlinkPressedForegroundThemeBrush" Color="#e6e6e6" />

但是当我悬停时,StackPanels Foreground 都没有改变

感谢帮助

4

1 回答 1

1

I don't think you can do that only with style. But, if you really want to use a style, you can combine with a behavior. The behavior can use PointerEntered and PointerExited events to set a new Background. Next, the behavior can be setted in your style. In order to implements this type of solution, you have to add the Behaviors SDK extensions (go to add references > Universal Windows > Extensions). The definition of the behavior (for example) :

public class HoverBehavior : DependencyObject, Microsoft.Xaml.Interactivity.IBehavior
{
    private Panel _associatedObject;
    private Brush _baseBrush;

    public Brush HoverBrush
    {
        get { return (Brush)GetValue(HoverBrushProperty); }
        set { SetValue(HoverBrushProperty, value); }
    }

    public static readonly DependencyProperty HoverBrushProperty =
        DependencyProperty.Register("HoverBrush", typeof(Brush), typeof(HoverBehavior), new PropertyMetadata(null));

    public Brush DefaultBrush
    {
        get { return (Brush)GetValue(DefaultBrushProperty); }
        set { SetValue(DefaultBrushProperty, value); }
    }

    public static readonly DependencyProperty DefaultBrushProperty =
        DependencyProperty.Register("DefaultBrush", typeof(Brush), typeof(HoverBehavior), new PropertyMetadata(null));

    public DependencyObject AssociatedObject
    {
        get { return _associatedObject; }
    }

    public void Attach(DependencyObject associatedObject)
    {
        _associatedObject = associatedObject as Panel;
        if (_associatedObject != null)
        {
            _baseBrush = _associatedObject.Background;
            if (_associatedObject != null)
            {
                _associatedObject.PointerEntered += _associatedObject_PointerEntered;
                _associatedObject.PointerExited += _associatedObject_PointerExited;
            }
        }
    }

    private void _associatedObject_PointerExited(object sender, PointerRoutedEventArgs e)
    {
        if (_associatedObject != null)
            _associatedObject.Background = HoverBrush;
    }

    private void _associatedObject_PointerEntered(object sender, PointerRoutedEventArgs e)
    {
        if (_associatedObject != null)
            _associatedObject.Background = DefaultBrush ?? _baseBrush;
    }

    public void Detach()
    {
        _associatedObject.PointerEntered -= _associatedObject_PointerEntered;
        _associatedObject.PointerExited -= _associatedObject_PointerExited;
    }
}

In the xaml, you have to add a "using" for the Interactivity namespace :

<Page xmlns:i="using:Microsoft.Xaml.Interactivity" />

And, you can define a new style (named SpStyle for example) :

<Style TargetType="StackPanel"
       x:Key="SpStyle">
    <Setter Property="i:Interaction.Behaviors">
        <Setter.Value>
            <i:BehaviorCollection>
                <local:HoverBehavior DefaultBrush="Yellow"
                                     HoverBrush="Green" />
            </i:BehaviorCollection>
        </Setter.Value>
    </Setter>
</Style>

The HoverBrush property of the behavior is the color of the StackPanel when the mouse pointer is hover the StackPanel, and DefaultBrush (can be unset) is the color of the StackPanel when the mouse pointer is not hover the StackPanel.

于 2015-12-19T18:56:17.990 回答