2010-07-20 120 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