2010-07-20 106 views
2

我正在处理菜单的一部分以允许其用户重新组织它。这个菜单有一个包含12个元素的网格(4x3),所有这些都是NavButton类型,我创建的类扩展了Button。在NavButton中,我有一个拖动功能,允许按钮在整个网格中移动,目前我正在确定每个按钮应该放在MouseEnter事件的哪个位置。WPF创建自定义事件

为了确定在NavButtons应重新定位,我已经把薄的矩形,将照亮上的MouseEnter,还是让我觉得这是他们应该如何工作。我的问题是,当NavButton被拖动到矩形上时,MouseEnter事件不会触发,因为鼠标位于Rectangle上方的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,因此任何有识之士将不胜感激。谢谢。

回答

1

这绝对是可能的,但它可能并不容易。

约什史密斯张贴this article,其包括用于与ListView实现该代码。他创建了一个附加属性“IsUnderDragCursor”,并显示了“拖动光标”下的项目变为蓝色的触发器。

很明显,你必须采取消息来源并使其适应你的菜单和NavButtons,但其原理都在那里。

+0

感谢那篇文章!它帮助我更深入地了解Drag Events的工作原理。 – 2010-08-09 20:21:58