wpf - 如何控制usercontrol的mousehover可见性?

时间:2015-12-18 01:55:45

标签: c# wpf xaml user-controls

我有一个用户控件,我想禁用UserControl中控件的可见性。当用户的光标悬停在用户控件的主要部分(即“橙色”矩形部分)上时,我只希望它可见。红色圆圈是控件的一部分,只能在“悬停”

上显示

enter image description here

MainWindow.xaml

Document

UserControl - ShapeNode.xaml

insertString(...)

3 个答案:

答案 0 :(得分:3)

我宁愿使用可以在UserControl中模板化的控件。我最喜欢的是一个按钮 - 这是因为如果有任何用途的点击事件。但你可以使用其他的。

<UserControl x:Class="WpfApplication1.ShapeNode"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
  <Button>
    <Button.Template>
        <ControlTemplate>
            <Grid x:Name="MyGrid" >
                <Ellipse x:Name="MyEllipse" Visibility="Hidden" Fill="Red" Opacity=".2" Height="150" Width="150"/>
                <Border Margin="5" Height="50" Width="100" Background="#FFDE6119" CornerRadius="5"></Border>
                <TextBlock VerticalAlignment="Center" Background="Transparent" Text="Donuts" HorizontalAlignment="Center"></TextBlock>
            </Grid>
        <ControlTemplate.Triggers>
            <Trigger SourceName="MyGrid" Property="IsMouseOver" Value="True">
                <Setter TargetName="MyEllipse" Property="Visibility" Value="Visible"></Setter>
            </Trigger>
        </ControlTemplate.Triggers>
        </ControlTemplate>
     </Button.Template>
  </Button>
</UserControl>

答案 1 :(得分:1)

  

的xmlns:I =&#34; HTTP://schemas.microsoft.com/expression/2010/interactivity"   的xmlns:EI =&#34; HTTP://schemas.microsoft.com/expression/2010/interactions"

<Grid x:Name="Grid1" Width="200" Margin="176,197,248,114" d:LayoutOverrides="HorizontalAlignment, VerticalAlignment">
            <Ellipse Fill="Red" Opacity=".2" Height="150" Width="150" Margin="25,0,25,-19"/>
            <Border Margin="50,40.5" Height="50" Width="100" Background="#FFDE6119" CornerRadius="5"/>
            <TextBlock VerticalAlignment="Center" Background="Transparent" Text="Donuts" HorizontalAlignment="Center">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseEnter">
                        <ei:ChangePropertyAction TargetObject="{Binding ElementName=Grid1}" PropertyName="Visibility">
                            <ei:ChangePropertyAction.Value>
                                <Visibility>Collapsed</Visibility>
                            </ei:ChangePropertyAction.Value>
                        </ei:ChangePropertyAction>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </TextBlock>
        </Grid>

答案 2 :(得分:0)

您可以使用binding来获得结果。 ShapeNode.xaml

中的椭圆的可见性属性绑定到Border

将您的border名称设置为例如“border1”并将可见性绑定放置为:

Visibility="{Binding Path=IsMouseOver, ElementName=border1, Converter={StaticResource boolToVisibillityConverter}}"

您必须创建转换器才能将布尔值更改为可见性。 使用以下转换器:

public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool flag = false;
        if (value is bool)
        {
            flag = (bool)value;
        }
        return (flag ? Visibility.Visible : Visibility.Hidden);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

请注意,在您的情况下,您必须返回隐藏而不是已折叠。否则,当可见性发生变化时,边框会改变位置。