Click here to Skip to main content
15,885,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a user control simulating water mark text box.
I need to allow drop text files on the user control.
The PreviewDragEnter event works fine. This is the event where i want to verify that the file that been drag is actually a text file.
The problem is that the drop event never fires.
I want to catch the event when the user release the mouse button and decide to drop the file into the user control.
I think it got something to do with the multiple controls inside the user control and the hierarchy.
I really don't care which control will fire the drop event as long as it actually fire.
This is the xaml (the control i want to target the drop event is name '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>


What I have tried:

I tried to use protected override drop event but it want fire also. Only the drag event fires.
Posted
Updated 28-Jul-20 10:10am

Found the answer in this link:
https://peteohanlon.wordpress.com/2009/09/28/textbox-dragdrop-in-wpf/
 
Share this answer
 
 
Share this answer
 
Comments
oronsultan 28-Jul-20 13:57pm    
Dear Gerry,
I'm not trying to cancel drag event, I'm trying to fire it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900