1

I have some idea but I can't make this happened. I have some TextBox in my wpf

<Button Content="Add" Command="{Binding AddAdditionalFileCommand}"  Name="MB" />
<TextBox  Text="{Binding SomeModel.File.FileName}" />

and in my vm

FileName = Path.GetFileName(file)

This textbox show the name of the loaded file with extension. And I can change this name of file. Ok. And my idea is when I click button to upload the file and after uploading file when in textbox appears the name of the file I want to focus automatically to this all name of file in textbox without extension. Can I do that something like this?

For instance Upload the file named test.mpkg and focus on test without mpkg. Like in Windows when you want to rename the file. It focus on file name without extension.

Edit My command in ViewModel

    private DelegateCommand addAdditionalFileCommand;
    public  DelegateCommand AddAdditionalFileCommand
    {
        get
        {
            if (addAdditionalFileCommand == null)
                addAdditionalFileCommand = new DelegateCommand(x =>
                {
                    var file = IOService.OpenFileDialog("", new string[] { "File|*.*" });
                    if (System.IO.File.Exists(file))
                    {
                        Model.Package = new ApplicationVersionPackage() {
                            FilePack = File.ReadAllBytes(file),
                            FileName = Path.GetFileName(file)
                        };


                    }


                });
            return addAdditionalFileCommand;
        }
        set
        {
            if (addAdditionalFileCommand != value)
            {
                addAdditionalFileCommand = value;
                NotifyPropertyChanged("AddAdditionalFileCommand");
            }
        }
    }

How pass this Select or Focus from name of textbox to this command

manhart
  • 19
  • 5
  • Use the `Select()` method, as shown [here](https://stackoverflow.com/questions/12094937/how-to-highlight-select-text-in-a-wpf-textbox-without-focus#12999236). – DonBoitnott Oct 09 '18 at 12:03

1 Answers1

1

You can use the Select() in order to make focus in your textbox after the file uploaded.

Do something like this.

textBoxFileName.Focus();

//The -3 is to avoid the extension maybe you can do a method to get the extension 
//length, because can has differents lengths (.jpg, .jpeg)

textBoxFileName.Select(0, Path.GetFileName(file).length -3); 

I don't know if you are familiarized with the MVVM Pattern (Model-View-ViewModel) used in WPF projects, but it will help you a lot to bind that kind of stuff into your textbox and other components.

MVVM explained

MVVM example and tutorial

Diego Bascans
  • 1,083
  • 1
  • 6
  • 14