我当前正在编写一个小应用程序,我在以下几点陷入困境:
我的按钮在代码中有几个参数,我想在单独的文本块中显示值。
当我将鼠标悬停在按钮上时,是否有可能更改文本块的可见性,以便仅在将鼠标悬停在按钮上时才能看到参数?
答案 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;
}