【问题标题】:How bubbling up of mouse over works in WPF?鼠标悬停在 WPF 中如何起泡?
【发布时间】:2018-05-29 10:49:21
【问题描述】:

我有一个列跨度为 2 的矩形,在网格行中有一个标签和一个按钮。

<Rectangle Grid.Row="2" Grid.ColumnSpan="2"></Rectangle>
<Label Grid.Row="2" Grid.Column="0">Product Two</Label>
<Button Grid.Row="2" Grid.Column="1" Click="Button2_Click">Select</Button>

当鼠标悬停在行上时,我打算更改矩形的填充颜色。但似乎鼠标悬停在标签或按钮上会阻止鼠标悬停在矩形上,这与 HTML 不同。

这是默认行为吗?什么是正确的方法?

编辑,网格的完整 XAML

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="3*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Grid.Resources >
        <Style TargetType="{x:Type Rectangle}">
            <Setter Property="Fill" Value="Transparent"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Fill" Value="#44000000"/>
                </Trigger>
            </Style.Triggers>
        </Style>            
    </Grid.Resources>

    <Label Grid.Row="0" Grid.Column="0" FontWeight="Bold">Product</Label>
    <Label Grid.Row="0" Grid.Column="1">Choose</Label>

    <Rectangle Grid.Row="1" Grid.ColumnSpan="2"></Rectangle>
    <Label Grid.Row="1" Grid.Column="0">Product One</Label>
    <Button Grid.Row="1" Grid.Column="1" Click="Button1_Click">Select</Button>
</Grid>

【问题讨论】:

  • 请告诉我们grid这3个项目在里面。
  • 为网格添加了完整的 xaml

标签: c# wpf xaml


【解决方案1】:

您想要完成的任务似乎可以通过将矩形替换为Grid 概述您的其他项目来解决。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="3*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Label Grid.Row="0" Grid.Column="0" FontWeight="Bold">Product</Label>
    <Label Grid.Row="0" Grid.Column="1">Choose</Label>

    <Grid Grid.Row="1" Grid.ColumnSpan="2">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.Resources >
            <Style TargetType="{x:Type Grid}">
                <Setter Property="Background" Value="Transparent"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#44000000"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Grid.Resources>
        <Label Grid.Row="1" Grid.Column="0">Product One</Label>
        <Button Grid.Row="1" Grid.Column="1">Select</Button>
    </Grid>
</Grid>

我想用这个 xaml 改变一些其他的东西,但这不是你的问题的一部分。

如果这给出了你想要的结果,请告诉我。

【讨论】:

  • 我想改变单行的背景。这个答案改变了整个网格的背景。
  • 好的,我现在看到了。我停在一排以缩短代码。实际上,我有八个。所以每一行都有一个网格,对吧?我们还需要父网格中的列定义吗?
  • 我现在要做的是使用 SharedSizeGroups,所以你将最外面的 Grid 设置为 Grid.IsSharedSizeScope = "True",然后在 ColumnDefinition 中设置 SharedSizeGroup = "",所以在您不必重新定义子网格的大小,只需说:SharedSizeGroup = "",因此您可以通过在一个位置更改它们来控制列大小。此外,您还可以查看 ListView,因为在我看来您正在尝试重新创建它的行为。
  • @Spongebrot 您可以尝试编辑我的答案以符合您的建议吗?这个 SharedSizeGroups 对我来说是新的,我似乎无法让它工作,或者至少不是我想要的。
  • @MikeHjortChristensen 我的错,忘记了 SharedSizeGroup 不适用于星号注释,但只能用于自动或固定大小:/
猜你喜欢
  • 2010-12-22
  • 1970-01-01
  • 2011-04-15
  • 1970-01-01
  • 2015-08-08
  • 1970-01-01
  • 2015-04-04
  • 1970-01-01
  • 2011-08-17
相关资源
最近更新 更多