locked
How to use CommandTarget with a custom RoutedCommand RRS feed

  • Question

  • Hi everybody,

    I'm encountering problems with a RoutedCommand.
    I have a RoutedCommand that looks like the following one (stored in a static class named Commands) :

    public static RoutedCommand MyRoutedCommand = new RoutedCommand();
    
    public static void SaveFile(object sender, ExecutedRoutedEventArgs e)
    {
                var dgSave = new Microsoft.Win32.SaveFileDialog();
                dgSave.Filter = "Google Maps Files (*.kml)|*.kml|All files (*.*)|*.*";
                dgSave.ShowDialog();
                // ??? What do I have to write here ?
    }

    In my XAML file, I have a Button beside a TextBox :

                    <TextBox x:Name="tboxFullPathFile" Width="300"/>
                    <Button x:Name="btBrowse" Content="Browse ..." Margin="5,0"
                            Command="{x:Static Member=m:Commands.MyRoutedCommand}" CommandTarget="{Binding ElementName=tboxFullPathFile, Path=Text}"/>

    In my XAML .cs file, I added the following line :

    CommandBindings.Add(new CommandBinding(Commands.MyRoutedCommand, Commands.SaveFile));

    I would like the tboxFullPathFile to be filled with the path selected in the function Save. But I don't know how I have to do and I can't use CommandParamter (I planned to use it to do other things).

    Do you have an idea ?

    Thanks in advance for your help.



    • Edited by NicolasC Sunday, May 12, 2013 12:29 AM
    Sunday, May 12, 2013 12:27 AM

Answers

  • xaml:

     <TextBox x:Name="tboxFullPathFile" Width="300"/>
            <Button x:Name="btBrowse" Content="Browse ..." Margin="5,0"
                            Command="{x:Static Member=m:Commands.MyRoutedCommand}" CommandTarget="{Binding ElementName=tboxFullPathFile}"/>
    

    xaml.cs:

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                tboxFullPathFile.CommandBindings.Add(new CommandBinding(Commands.MyRoutedCommand, Commands.SaveFile));
            }
        }

    Commands.cs:

        public static class Commands
        {
            public static RoutedCommand MyRoutedCommand = new RoutedCommand();
    
            public static void SaveFile(object target, ExecutedRoutedEventArgs e)
            {
                var dgSave = new Microsoft.Win32.SaveFileDialog();
                dgSave.Filter = "Google Maps Files (*.kml)|*.kml|All files (*.*)|*.*";
                dgSave.ShowDialog();
                // ??? What do I have to write here ?
                var textBox = target as System.Windows.Controls.TextBox;
                if (textBox != null)
                {
                    textBox.Text = dgSave.FileName;
                } 
            }
        }
    


    • Marked as answer by NicolasC Sunday, May 12, 2013 3:52 PM
    Sunday, May 12, 2013 2:07 AM
  • Hi Adnan,

    Thanks for your answer.
    A question about your code : Must the target be a control ? or can it be a simple String by example ?

    CommandTarget must be IInputElement.

    http://msdn.microsoft.com/de-de/library/system.windows.input.routedcommand.execute%28v=vs.85%29.aspx

    • Marked as answer by NicolasC Sunday, May 12, 2013 3:52 PM
    Sunday, May 12, 2013 2:21 PM

All replies

  • xaml:

     <TextBox x:Name="tboxFullPathFile" Width="300"/>
            <Button x:Name="btBrowse" Content="Browse ..." Margin="5,0"
                            Command="{x:Static Member=m:Commands.MyRoutedCommand}" CommandTarget="{Binding ElementName=tboxFullPathFile}"/>
    

    xaml.cs:

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                tboxFullPathFile.CommandBindings.Add(new CommandBinding(Commands.MyRoutedCommand, Commands.SaveFile));
            }
        }

    Commands.cs:

        public static class Commands
        {
            public static RoutedCommand MyRoutedCommand = new RoutedCommand();
    
            public static void SaveFile(object target, ExecutedRoutedEventArgs e)
            {
                var dgSave = new Microsoft.Win32.SaveFileDialog();
                dgSave.Filter = "Google Maps Files (*.kml)|*.kml|All files (*.*)|*.*";
                dgSave.ShowDialog();
                // ??? What do I have to write here ?
                var textBox = target as System.Windows.Controls.TextBox;
                if (textBox != null)
                {
                    textBox.Text = dgSave.FileName;
                } 
            }
        }
    


    • Marked as answer by NicolasC Sunday, May 12, 2013 3:52 PM
    Sunday, May 12, 2013 2:07 AM
  • Hi Adnan,

    Thanks for your answer.
    A question about your code : Must the target be a control ? or can it be a simple String by example ?

    Sunday, May 12, 2013 10:14 AM
  • Hi Adnan,

    Thanks for your answer.
    A question about your code : Must the target be a control ? or can it be a simple String by example ?

    CommandTarget must be IInputElement.

    http://msdn.microsoft.com/de-de/library/system.windows.input.routedcommand.execute%28v=vs.85%29.aspx

    • Marked as answer by NicolasC Sunday, May 12, 2013 3:52 PM
    Sunday, May 12, 2013 2:21 PM