Click here to Skip to main content
15,560,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I hope I can explain this, I have such a hard time and I'm not sure that there's really an answer but;

I'm well into developing an WPF app and am going back through and cleaning up, fixing bugs and doing all the tedious work.

I have an image of a Component that can either be populated by; clicking a button and browsing to the image, or drag an image onto a StackPanel that contains a Target image.

XML
<StackPanel Width="20" Height="20" Margin="15 0 0 0" AllowDrop="True">
     <i:Interaction.Triggers>
         <i:EventTrigger EventName="Drop">
           <i:InvokeCommandAction Command="{Binding DropCommand}"
                   PassEventArgsToCommand="True"/>
           </i:EventTrigger>
      </i:Interaction.Triggers>
      <Image Source="pack://application:,,,/Resources/Images/target.ico" Width="22"/>
</StackPanel>


and this is the Drop relay command
C#
private void ExecuteDropCommand(object param)
{
    DragEventArgs args = param as DragEventArgs;
    if (args.Data.GetDataPresent(DataFormats.FileDrop))
    {
        string[] file = (string[])args.Data.GetData(DataFormats.FileDrop);
        string ext = Path.GetExtension(file[0]);
        if (ext == ".png" || ext == ".jpg" || ext == ".bmp")
        {
             FileRenameWindow dlg = new FileRenameWindow(file[0]);
             if (dlg.ShowDialog() == true)
                    ProcessImage(file[0], dlg.Filename + ext);
         }
     }
     args.Handled = true;
}


and this works very well. So the problem came when I decided to rename the files I was pulling in from the Web Browser, in my case Firefox.

In the Drop event I display a window with a TextBox to enter filename and Accept and Cancel buttons. This was working fine also but I wanted to set the Focus to the TextBox and have tried everything under the sun and what I got googling and trying for the last 2 hours. The Focus code I'm trying works everywhere else so I know its not that.

In my xaml file I have
C#
Loaded="FileRenameWindow_Loaded"


and in code behind I've got;
C#
private void FileRenameWindow_Loaded(object sender, RoutedEventArgs e)
{
    FilenameTextbox.Focus();
    FilenameTextbox.SelectAll();
}

I've implemented MVVM and this is the only case where I do anything like this.

So the problem is that the Drop hangs up the Browser until the File rename window is dismissed and the Focus doesn't work.

But the odd thing is that if I do the Drop for the second time the focus works.
The browser still hangs but things work as I wanted.

So I have to either finalize the drop somehow or find another way to force the focus. I'm under the impression that the window Loaded event is the place to do this but the interaction between the drop and the window is somehow at odds.

What I have tried:

In my xaml file I have
<pre lang="C#">Loaded="FileRenameWindow_Loaded"


and in code behind I've got;
C#
private void FileRenameWindow_Loaded(object sender, RoutedEventArgs e)
{
    FilenameTextbox.Focus();
    FilenameTextbox.SelectAll();
}
Posted
Updated 16-Oct-22 11:16am
v2
Comments
Gerry Schmitz 17-Oct-22 1:46am    
Run your rename asynchronously; the modal dialog is otherwise holding up the drop (and anything else waiting on it).

https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatcher.begininvoke?view=windowsdesktop-6.0
Mike Hankey 17-Oct-22 8:17am    
Thanks Gerry I'll try 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