Silverlight的;在鼠标悬停时更改网格的背景

时间:2014-10-18 12:38:52

标签: c# silverlight triggers grid routedevent

当鼠标进入时,我想要更改网格的背景颜色(在Silverlight中),当鼠标离开时重置它。 所以我尝试了不同的方法但没有成功。这是我尝试过的:

1:使用EventTriggers:

<Grid.Triggers>
    <EventTrigger RoutedEvent="MouseEnter">
        <BeginStoryboard Storyboard="{StaticResouce mouseEnter}"/>
    </EventTrigger>
</Grid.Triggers>

这不起作用并说:

  

成员“IsMouseOver”无法识别或无法访问

2。使用Style.Triggers

我尝试在带有TargetType="Grid"的样式中设置一些简单的触发器,但在Silverlight中似乎无法在XAML中创建Style.Triggers。这是代码:

<Grid.Style>
    <Style TargetType="Grid">
        <Style.Triggers>
        </Style.Triggers>
    </Style>
</Grid.Style>

但它说:

  

在“风格”类型中找不到可附加属性“触发器”。

第3。使用交互库

我还使用了Interactivity.dll和interaction.dll,但他们也没有“工作”。

任何人都可以帮助当鼠标进入Silverlight时如何更改网格背景?

1 个答案:

答案 0 :(得分:1)

有三种可能的解决方案:

第一个解决方案:使用VisualSates:在Silverlight中更改Background上的MouseOver可以通过VisualStates完成。 这是一个例子:

<UserControl class="MyUserControlWithVisualStates">
    <Grid x:Name="RootGrid" Background="UglyRed">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal"/>
                <VisualState x:Name="Disabled"/>
                <VisualState x:Name="MouseOver">
                  <Storyboard>
                    <ColorAnimation To="Green"
                 Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
                 Storyboard.TargetName="RootGrid"/>
                  </Storyboard>
                </VisualState>
            </VisualStateGroup>

            <VisualStateGroup x:Name="FocusStates">
                <VisualState x:Name="Focused"/>
                <VisualState x:Name="Unfocused"/>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

        <OtherGridContent ... />

    </Grid>
</UserControl>

和代码背后:

public partial class MyUserControlWithVisualStates : UserControl
{
    private bool m_isMouseOver;
    public MyUserControlWithVisualStates()
    {
        InitializeComponent();
        RootGrid.MouseEnter += OnRootGridMouseEnter;
        RootGrid.MouseLeave += OnRootGridMouseLeave;
    }

    private void UpdateVisualStates()
    {
        if ( m_isMouseOver )
            VisualStateManager.GoToState( this, "MouseOver", true );
        else
            VisualStateManager.GoToState( this, "Normal", true );
    }

    private void OnRootGridMouseLeave( object sender, MouseEventArgs e )
    {
        m_isMouseOver = false;
        UpdateVisualStates();
    }

    private void OnRootGridMouseEnter( object sender, MouseEventArgs e )
    {
        m_isMouseOver = true;
        UpdateVisualStates();
    }
}

第二个解决方案:通过代码隐藏更改属性: MouseEnter和MouseLeave事件处理程序只能更改网格的背景颜色。

public partial class MyUserControl : UserControl
{
    private bool m_isMouseOver;
    public MyUserControl()
    {
        InitializeComponent();
        RootGrid.MouseEnter += OnRootGridMouseEnter;
        RootGrid.MouseLeave += OnRootGridMouseLeave;
    }

    private void UpdateBackground()
    {
        if (m_isMouseOver)
            ((SolidColorBrush) RootGrid.Background).Color = Colors.Red;
        else
            ((SolidColorBrush) RootGrid.Background).Color = Colors.Green;
    }

    private void OnRootGridMouseLeave( object sender, MouseEventArgs e )
    {
        m_isMouseOver = false;
        UpdateBackground();
    }

    private void OnRootGridMouseEnter( object sender, MouseEventArgs e )
    {
        m_isMouseOver = true;
        UpdateBackground();
    }
}

第三种解决方案:在xaml中使用触发器和操作:

的xmlns:ⅰ= “http://schemas.microsoft.com/expression/2010/interactivity” 的xmlns:EI = “http://schemas.microsoft.com/expression/2010/interactions”

<Grid x:Name="TheGrid" Background="Blue">
    <Grid.Resources>
        <SolidColorBrush x:Key="MouseOverBrush" Color="Green"/>
        <SolidColorBrush x:Key="NormalBrush" Color="Red"/>
    </Grid.Resources>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseEnter" SourceName="TheGrid">
            <ei:ChangePropertyAction
                TargetName="TheGrid"
                PropertyName="Background"
                Value="{StaticResource MouseOverBrush}"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="MouseLeave" SourceName="TheGrid">
            <ei:ChangePropertyAction
                TargetName="TheGrid"
                PropertyName="Background"
                Value="{StaticResource NormalBrush}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Grid>