0

我有一个模拟水印文本框的用户控件。我需要允许在用户控件上放置文本文件。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>
4

1 回答 1

0

在这里找到答案: https ://peteohanlon.wordpress.com/2009/09/28/textbox-dragdrop-in-wpf/ 显然,将拖放到 WPF 中的文本框实际上不起作用。当您尝试将项目拖放到 TextBox 中时,它会拒绝配合并将鼠标光标保留为 Drop denied 光标,并且您无法拖放到该字段中。(顺便说一下,这种行为也适用于 RichTextBox 和 FlowDocument 控件)。即使您将 AllowDrop 设置为 true,您也无法放入这些字段的原因是这些特定控件将拖放事件标记为已处理,从而阻止您自己处理它们。

于 2020-07-29T07:31:00.537 回答