按钮悬停时,文本块需要在 c# 中显示一些文本

问题描述 投票:0回答:1

我有一个按钮。当鼠标悬停在按钮上时,我的项目中使用的文本块应该显示一些值。按钮是在库和我在项目中使用的库中定义的。在我的项目中,我定义了文本块。因此,该按钮是使用代码创建的,在我的项目中,将其悬停时,文本块应该显示一些值。如何实现这一目标。我听说使用依赖属性可以实现它。我是 wpf 新手,我不知道该怎么做。你能帮忙吗?

在库中我添加了以下代码,其余我不知道该怎么做。我正在使用 MVVM。

<Button.Style>
    <Style TargetType="{x:Type Button}">
        <Style.Triggers>
            <Trigger  Property="IsMouseOver" Value="True">
               
            </Trigger>
        </Style.Triggers>
    </Style>
</Button.Style>
c# wpf triggers hover textblock
1个回答
0
投票

好吧,我使用

DataTrigger
轻松做到了。但是,我不知道您的文本块的文本来自哪里,所以我假设它是静态的。我已经演示了触发两个文本块属性
Text
Visibility
的设置器,即使它看起来是多余的,只是为了给您一个想法和一些选择。

DataTrigger
绑定中引用不同元素的关键是使用
ElementName
,请动态注意名称
trigger</code in Binding = "{Binding ElementName = trigger, Path = IsMouseOver}".

Binding = "{Binding ElementName = trigger, Path = IsMouseOver}"


Just for comparison, I also added a regular Style.Triggers Trigger to trigger the background color of the same element, the Button instance.

Here is a complete working XAML sample with no code behind involved:

<Window x:Class="SampleWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    Title="SampleWindow" Height="450" Width="800"
    ResizeMode="CanResizeWithGrip">
<Window.Resources>
    <Thickness x:Key="Padding">4</Thickness>
    <Style x:Key="Border" TargetType="Border">
        <Setter Property="BorderThickness" Value="3"/>
        <Setter Property="Padding" Value="4"/>
    </Style>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Padding" Value="4 0 4 0"/>
        <Style.Triggers>
            <Trigger  Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Yellow"/>
            </Trigger>
                    <Trigger  Property="IsMouseOver" Value="False">
                <Setter Property="Background" Value="AliceBlue"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Border Padding="{StaticResource Padding}">
        <TextBlock>
            <TextBlock.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding = "{Binding ElementName = trigger, Path = IsMouseOver}" Value = "true">
                            <Setter Property = "TextBlock.Text" Value = "Before clicking on this button, think twice"/>
                            <Setter Property = "TextBlock.Visibility" Value = "Visible"/>
                        </DataTrigger>
                        <DataTrigger Binding = "{Binding ElementName = trigger, Path = IsMouseOver}" Value = "false">
                            <Setter Property = "TextBlock.Text" Value = ""/>
                            <Setter Property = "TextBlock.Visibility" Value = "Collapsed"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </Border>
    <Border Grid.Row="1" BorderBrush="Navy" BorderThickness="3" Style="{StaticResource Border}" Margin="3">
        <TextBlock>Some useful content</TextBlock>
    </Border>
    <StatusBar Grid.Row="2" Padding="{StaticResource Padding}">
        <StatusBarItem>
            <Button x:Name="trigger">Click it!</Button>
        </StatusBarItem>
    </StatusBar>
</Grid>
,您必须在后面的代码中完成所有操作。

谢谢。
—SA

© www.soinside.com 2019 - 2024. All rights reserved.