TextBlock可见性通过Button悬停更改WPF

时间:2014-12-03 08:41:54

标签: c# wpf hover visibility

我当前正在编写一个小应用程序,我在以下几点陷入困境:

我的按钮在代码中有几个参数,我想在单独的文本块中显示值。

当我将鼠标悬停在按钮上时,是否有可能更改文本块的可见性,以便仅在将鼠标悬停在按钮上时才能看到参数?

2 个答案:

答案 0 :(得分:2)

XAML解决方案:

<Grid>
    <Grid.Resources>
        <converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <TextBlock Text="Foo" Visibility="{Binding IsMouseOver, ElementName=Foo, Converter={StaticResource BooleanToVisibilityConverter}}"/>
    <TextBlock Grid.Row="1" Text="Bar" Visibility="{Binding IsMouseOver, ElementName=Bar, Converter={StaticResource BooleanToVisibilityConverter}}"/>

    <Button x:Name="Foo" Grid.Column="1" Content="Foo"/>
    <Button x:Name="Bar" Grid.Row="1" Grid.Column="1" Content="Bar"/>
</Grid>

其中converters名称空间声明为:

xmlns:converters="clr-namespace:System.Windows.Controls;assembly=PresentationFramework" 

答案 1 :(得分:0)

确实有,使用按钮MouseEnter事件。像下面这样的东西可以正常工作:

button1_MouseEnter(object sender, EventArgs e)
{
    textBlock1.Visibility = Visibility.Visible;
}

然后将其隐藏起来,使用MouseLeave事件:

button1_MouseLeave(object sender, EventArgs e)
{
    textBlock1.Visibility = Visibility.Hidden;
}