1

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

1:使用事件触发器:

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

这不起作用并说:

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

2. 使用 Style.Triggers

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

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

但它说:

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

3.使用交互库

我也使用了Interactivity.dll 和interaction.dll,但它们也不起作用。

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

4

1 回答 1

1

有三种可能的解决方案:

第一个解决方案:使用 VisualSates:在 Silverlight 中更改 a BackgroundonMouseOver可以通过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:i="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>
于 2014-10-20T07:28:21.733 回答