簡體   English   中英

WPF創建自定義事件

[英]WPF Creating Custom Events

我正在處理菜單的一部分,以允許其用戶對其進行重新組織。 這個菜單有一個包含12個元素的grid(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還是很陌生,所以任何見識都會受到贊賞。 謝謝。

絕對有可能,但可能並不容易。

Josh Smith發表了這篇文章 ,其中包括使用ListView實現此功能的代碼。 他創建了一個附加屬性“ IsUnderDragCursor”,並顯示了一個觸發器,其中“拖動光標”下的項目變為藍色。

顯然,您必須獲取源代碼並對其進行調整,以使其與菜單和NavButtons一起使用,但是原理就在那里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM