views:

25

answers:

1

I'm trying to drop a file into a textbox on my WPF application but it won't work. I believe I have the XAML setup correctly to do this, and a PreviewDragOver event handler does work-- just not Drop or PreviewDrop. Here's the XAML in question:

<Window x:Class="TableTagCount.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" AllowDrop="True">
<Grid Name="bgGrid" Drop="bgGrid_Drop" AllowDrop="True">
    <Grid.Background>
        <ImageBrush ImageSource="/TableTagCount;component/Images/Sunset.jpg" Stretch="Fill" TileMode="None" />
    </Grid.Background>
    <Button Content="Analyze" Height="23" HorizontalAlignment="Left" Margin="32,91,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click"/>
    <TextBox Height="23"  HorizontalAlignment="Left" Margin="32,43,0,0" Name="textBox1" VerticalAlignment="Top" Width="205"  PreviewDragOver="textBox1_PreviewDragOver" AllowDrop="True" />
    <Label Content="File Name" Height="28" HorizontalAlignment="Left" Margin="32,13,0,0" Name="label1" VerticalAlignment="Top" Width="65" />
</Grid></Window>

Note that the Drop operation does work on my Grid.

Also, I'd like to prevent the Drop event from being handled by the Grid if my textbox handles it first. In my textbox Drop handler I'm setting the DragEventArg's Handled property to true. Is this enough to keep the event from bubbling up to the Grid?

+1  A: 

First, I don't see a Drop handler on your TextBox. Could that be the reason it doesn't work? ;-)

Second, yes, setting Handled to true should be enough to stop bubbling.

Fyodor Soikin
Sorry for the confusion, I took out the drop handler because it wasn't working. Just to triple-check I added it back in and it's still not firing, or my event handler for it isn't picking it up. The PreviewDragOver handler works fine when I set a breakpoint on it.
larryq
Ok then, please describe what happens more specifically. "It won't work" doesn't sound very descriptive, does it?
Fyodor Soikin
Ok, here goes: when I drag a file from windows explorer onto my application, I'll see the rectangular icon that indicates I can drop it. When my cursor goes over my textbox however, the circle with the diagonal line through it appears and if I release my mouse button no drop event is fired.If I remove the Drop event handler in my XAML and use PreviewDragDrop instead, the filename appears in the textbox as soon as the cursor hits it.
larryq
And what exactly does your PreviewDragOver handler do?
Fyodor Soikin