【发布时间】:2015-05-13 20:46:18
【问题描述】:
我有一个带有 UserControl 的窗口,该 UserControl 有一个元素。 如果我将鼠标移到 UserControl 内的元素上,我希望 Window 内的其他一些元素做出反应(例如:隐藏它们)。
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:local="clr-namespace:WpfApplication4"
>
<DockPanel>
<Grid Width="50" Height="50" Background="Yellow">
<Grid.Style>
<Style TargetType="Grid">
<Style.Triggers>
<!-- When hovering innerControl inside userControl this grid should become invisible, of course this doesn't work, it hides the element if I move the mouse over any part of the usercontrol-->
<DataTrigger Binding="{Binding userControl.IsMouseOver, ElementName=userControl}" Value="True">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
</Grid>
<local:userControl x:Name="userControl" Width="300" Height="300"/>
</DockPanel>
用户控制
<UserControl x:Class="WpfApplication4.userControl"
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"
Background="Red"
>
<Grid>
<Grid x:Name="innerControl" Background="Blue" Width="100" Height="100"/>
</Grid>
public partial class userControl : UserControl
{
public userControl()
{
InitializeComponent();
}
//This is far from working
public new bool IsMouseOver
{
get { return (bool)GetValue(IsMouseOverProperty); }
set { SetValue(IsMouseOverProperty, value); }
}
public new static readonly DependencyProperty IsMouseOverProperty =
Border.IsMouseOverProperty.AddOwner(typeof(Border));
}
我知道我必须使用依赖属性,但我无法将我找到的示例用于此问题。
【问题讨论】:
-
你为什么要自己制作“IsMouseOver”属性?我很确定所有 FrameworkElements 都固有地具有该属性。
-
是的,那个代码是错误的。 ( //这远非工作)
-
关于你原来的问题,我想说这可能是不可能的,除非你再次创建所有标准的“IsMouseOver”代码,并让它对内部控件做出反应UserControl 作为一个整体。这似乎需要做很多工作,所以我建议您只在原始 xaml 视图中完成工作。
-
感谢您的建议。
标签: c# wpf xaml dependency-properties ismouseover