WPF 创建自定义事件
我正在研究菜单的一部分,以允许用户重新组织它。该菜单有一个包含 12 个元素的网格 (4x3),所有元素都是 NavButton 类型,这是我创建的一个扩展 Button 的类。在 NavButton 中,我有一个拖动功能,允许按钮在整个网格中移动,目前我正在努力确定每个按钮应落在 MouseEnter 事件上的位置。
为了确定 NavButtons 应该重新定位的位置,我放置了细长的矩形,这些矩形会在 MouseEnter 上变亮,或者所以我认为这就是它们应该如何工作。我的问题是,当将 NavButton 拖动到矩形上时,MouseEnter 事件不会触发,因为鼠标位于矩形上方的 NavButton 上。抱歉,如果这听起来令人困惑,但我将在下面发布代码。
综上所述,我想知道是否可以创建一个事件来确定 NavButton 何时“进入”矩形,类似于 MouseEnter 事件的工作原理?
C#:
private void rectangle_MouseEnter(object sender, MouseEventArgs e)
{
foreach (UIElement uie in this.grids[tabNavigation.SelectedIndex].Children)
{
if (uie.GetType() == typeof(NavButton_UC))
{
if ((uie as NavButton_UC).IsDragging)
{
(sender as Rectangle).Fill = Brushes.Gray;
}
}
}
}
private void rectangle_MouseLeave(object sender, MouseEventArgs e)
{
(sender as Rectangle).Fill = Brushes.Black;
}
XAML:
//The Style is in my ResourceDictionary
<Style TargetType="Rectangle">
<Setter Property="Fill" Value="Black" />
<Setter Property="Height" Value="151" />
<Setter Property="Width" Value="10" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Opacity" Value=".6" />
<EventSetter Event="MouseEnter" Handler="rectangle_MouseEnter" />
<EventSetter Event="MouseLeave" Handler="rectangle_MouseLeave" />
</Style>
...
<Grid Name="grid" Background="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="22"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Grid.Column="0" Name="rect00" />
<Rectangle Grid.Row="0" Grid.Column="1" Name="rect01" />
<Rectangle Grid.Row="0" Grid.Column="2" Name="rect02" />
<Rectangle Grid.Row="1" Grid.Column="0" Name="rect10" />
<Rectangle Grid.Row="1" Grid.Column="1" Name="rect11" />
<Rectangle Grid.Row="1" Grid.Column="2" Name="rect12" />
<Rectangle Grid.Row="2" Grid.Column="0" Name="rect20" />
<Rectangle Grid.Row="2" Grid.Column="1" Name="rect21" />
<Rectangle Grid.Row="2" Grid.Column="2" Name="rect22" />
<Rectangle Grid.Row="3" Grid.Column="0" Name="rect30" />
<Rectangle Grid.Row="3" Grid.Column="1" Name="rect31" />
<Rectangle Grid.Row="3" Grid.Column="2" Name="rect32" />
<TextBlock Grid.Row="5" Grid.Column="3" Name="TB" />
</Grid>
我对 WPF 相当陌生,因此任何见解将不胜感激。谢谢。
I am working on a part of a menu to allow it to be re-organized by its user. This menu has a grid(4x3) with 12 elements, all of which are of the type NavButton, a class I created that extends Button. In the NavButton I have a drag feature that allows the buttons to be moved throughout the grid and currently I am working on determining where each button should land on the MouseEnter event.
In order to determine where the NavButtons should be re-positioned, I have placed thin rectangles that will brighten on MouseEnter, or so I thought that is how they should work. My issue is that as the NavButton is dragged over the rectangles the MouseEnter event will not fire as the Mouse is over the NavButton which is over the Rectangle. Sorry if that sounds confusing, but I will post code below.
All of that said, I am wondering if it is possible at all to create an event to determine when the NavButton "Enters" the Rectangle, similar to how the MouseEnter event works?
C#:
private void rectangle_MouseEnter(object sender, MouseEventArgs e)
{
foreach (UIElement uie in this.grids[tabNavigation.SelectedIndex].Children)
{
if (uie.GetType() == typeof(NavButton_UC))
{
if ((uie as NavButton_UC).IsDragging)
{
(sender as Rectangle).Fill = Brushes.Gray;
}
}
}
}
private void rectangle_MouseLeave(object sender, MouseEventArgs e)
{
(sender as Rectangle).Fill = Brushes.Black;
}
XAML:
//The Style is in my ResourceDictionary
<Style TargetType="Rectangle">
<Setter Property="Fill" Value="Black" />
<Setter Property="Height" Value="151" />
<Setter Property="Width" Value="10" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Opacity" Value=".6" />
<EventSetter Event="MouseEnter" Handler="rectangle_MouseEnter" />
<EventSetter Event="MouseLeave" Handler="rectangle_MouseLeave" />
</Style>
...
<Grid Name="grid" Background="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="22"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Grid.Column="0" Name="rect00" />
<Rectangle Grid.Row="0" Grid.Column="1" Name="rect01" />
<Rectangle Grid.Row="0" Grid.Column="2" Name="rect02" />
<Rectangle Grid.Row="1" Grid.Column="0" Name="rect10" />
<Rectangle Grid.Row="1" Grid.Column="1" Name="rect11" />
<Rectangle Grid.Row="1" Grid.Column="2" Name="rect12" />
<Rectangle Grid.Row="2" Grid.Column="0" Name="rect20" />
<Rectangle Grid.Row="2" Grid.Column="1" Name="rect21" />
<Rectangle Grid.Row="2" Grid.Column="2" Name="rect22" />
<Rectangle Grid.Row="3" Grid.Column="0" Name="rect30" />
<Rectangle Grid.Row="3" Grid.Column="1" Name="rect31" />
<Rectangle Grid.Row="3" Grid.Column="2" Name="rect32" />
<TextBlock Grid.Row="5" Grid.Column="3" Name="TB" />
</Grid>
I am fairly new to WPF, so any insight would be appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这绝对是可能的,但可能并不容易。
Josh Smith 发布了这篇文章,其中包含使用
实现此功能的代码列表视图
。他创建了一个附加属性“IsUnderDragCursor”,并显示一个触发器,其中“拖动光标”下的项目变成蓝色。显然,您必须获取源代码并对其进行调整以使其与您的菜单和导航按钮一起使用,但原理都在那里。
It is definitely possible, but it may not be easy.
Josh Smith posted this article that includes code for implementing this with a
ListView
. He's created an attached property, "IsUnderDragCursor," and shows a trigger where the item under the "drag cursor" turns blue.Obviously, you'd have to take the source and adapt it to work with your menu and NavButtons, but the principles are all there.