繁体   English   中英

Wpf 掉落事件未触发

[英]Wpf drop event not firing

我有一个模拟水印文本框的用户控件。 我需要允许在用户控件上放置文本文件。 PreviewDragEnter 事件工作正常。 这是我想验证被拖动的文件实际上是文本文件的事件。 问题是 drop 事件永远不会触发。 我想在用户释放鼠标按钮并决定将文件放入用户控件时捕获事件。 我认为这与用户控件和层次结构中的多个控件有关。 我真的不在乎哪个控件会触发 drop 事件,只要它实际触发即可。 这是 xaml (我要针对放置事件的控件名称为“txt”)

<UserControl x:Class="CrumbSearch.CustomControls.package_watermark"
             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" 
             Height="45" Width="Auto" 
             FontFamily="B Yekan"             
             AllowDrop="true"
             Grid.IsSharedSizeScope="True"              
             xmlns:wpfwatermarktextbox="clr-namespace:WPFWaterMarkTextBox;assembly=WPFWaterMarkTextBox">
    <FrameworkElement.Resources>
        <ResourceDictionary>
            <FontFamily x:Key="simple_line_icon">/fonts/Simple-Line-Icons.ttf#simple-line-icons</FontFamily>
            <Style x:Key="SimpleTextBox" TargetType="{x:Type TextBox}">
                <Setter Property="AllowDrop" Value="True"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TextBox}">
                            <Border Name="border" BorderBrush="{TemplateBinding BorderBrush}" 
                                    BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                <ScrollViewer Name="PART_ContentHost" Focusable="False" Margin="2,10,0,0"
                                              AllowDrop="True"
                                              HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="UIElement.IsFocused" Value="True">
                                    <Setter TargetName="border" Property="Border.BorderBrush" Value="Transparent" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="SimplePasswordBox" TargetType="{x:Type PasswordBox}">
                <Setter Property="AllowDrop" Value="True"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type PasswordBox}">
                            <Border Name="border" BorderBrush="{TemplateBinding BorderBrush}" 
                                    BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" 
                                    SnapsToDevicePixels="True">
                                <ScrollViewer Name="PART_ContentHost" 
                                              Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="UIElement.IsFocused" Value="True">
                                    <Setter TargetName="border" Property="Border.BorderBrush" Value="#00000000" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </FrameworkElement.Resources>
    <Border Name="border" BorderThickness="0.5" CornerRadius="4" MouseEnter="border_MouseEnter"             
            MouseLeave="border_MouseLeave">
        <FrameworkElement.Style>
            <Style TargetType="{x:Type Border}">
                <Setter Property="BorderBrush" Value="White" />
                <Setter Property="AllowDrop" Value="true" />
            </Style>
        </FrameworkElement.Style>
        <Grid Name="MainGrid" PreviewMouseLeftButtonDown="MainGrid_PreviewMouseLeftButtonDown"               
              LostFocus="MainGrid_LostFocus">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Label Name="lbl2" Padding="5,0" FontSize="10" Content="Name" 
                   Margin="0,5,0,0"                   
                   HorizontalAlignment="Left" Foreground="White" FlowDirection="LeftToRight"
                   VerticalAlignment="Top" Height="13" Grid.IsSharedSizeScope="True" 
                   Opacity="0" Grid.Column="1" Background="{x:Null}" />
            <TextBlock Name="lbl" Text="WaterMark" HorizontalAlignment="Left" 
                       Foreground="#0EE7EC" FontSize="12"                        
                       Padding="5,5,5,0" FlowDirection="LeftToRight" FontFamily="Andalus"
                       TextWrapping="WrapWithOverflow" FontStyle="Italic"
                       Opacity="0.7" Grid.Column="1" 
                       VerticalAlignment="Center" Background="{x:Null}" />
            <TextBox Name="txt" Style="{DynamicResource SimpleTextBox}" FlowDirection="LeftToRight"                                                                        
                     AllowDrop="true" Background="{x:Null}" 
                     PreviewDragEnter="txt_PreviewDragEnter"
                     PreviewDrop="txt_PreviewDrop" Drop="txt_Drop"
                     TextWrapping="NoWrap" VerticalContentAlignment="Center" FontSize="14" 
                     VerticalScrollBarVisibility="Disabled" MaxLines="1" FontWeight="Medium" Grid.Column="1" 
                     BorderBrush="{x:Null}" TextChanged="TextBox_TextChanged"  Foreground="White"
                     PreviewGotKeyboardFocus="txt_PreviewGotKeyboardFocus" 
                     PreviewLostKeyboardFocus="txt_PreviewLostKeyboardFocus" />
            <PasswordBox Name="pass" Style="{DynamicResource SimplePasswordBox}" FontSize="14"                          
                         VerticalContentAlignment="Center" 
                         ScrollViewer.VerticalScrollBarVisibility="Disabled" Grid.Column="1" 
                         Visibility="Collapsed" BorderBrush="{x:Null}" 
                         Background="{x:Null}" PasswordChanged="PasswordBox_PasswordChanged" />
            <Label Name="LblIcon" Padding="0" FontFamily="{DynamicResource simple_line_icon}"                     
                   VerticalContentAlignment="Center" HorizontalContentAlignment="Center" 
                   Foreground="#FF6A6A6A" FontSize="14" Background="{x:Null}" />
        </Grid>
    </Border>
</UserControl>

Found the answer here: https://peteohanlon.wordpress.com/2009/09/28/textbox-dragdrop-in-wpf/ Apparently, Drag Drop into a TextBox in WPF doesn't actually work. 当您尝试将项目拖放到 TextBox 中时,它拒绝合作并离开鼠标 cursor 因为 Drop denied cursor 并且您不能拖放到该字段中。 (顺便说一下,这种行为也适用于 RichTextBox 和 FlowDocument 控件)。 即使您将 AllowDrop 设置为 true,您也无法放入这些字段的原因是这些特定控件将拖放事件标记为已处理,从而阻止您自己处理它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM