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.
<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
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
Loaded="FileRenameWindow_Loaded"
and in code behind I've got;
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;
private void FileRenameWindow_Loaded(object sender, RoutedEventArgs e)
{
FilenameTextbox.Focus();
FilenameTextbox.SelectAll();
}