locked
how set button shortcut key in user control RRS feed

  • Question

  • Hi,

       Now i want know how to set button shortcut key in user control in wpf environment...

    Monday, August 5, 2013 6:21 AM

All replies

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Windows.Forms;
    using System.IO;
    using System.Xml.Linq;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();          
            }
    
    
            private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
            {
                if (Key.L.Equals(e.Key) && Keyboard.Modifiers.Equals(ModifierKeys.Control))
                {
                    txtblk.Text = "Data Saved Successfully";
                }
            }
        }
    
    
    
    }
    
    <Window x:Class="WpfApplication1.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1" Height="300" Width="300" KeyUp="Window_KeyUp">
        <Window.Resources>
         
        </Window.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="200"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>       
            <TextBlock x:Name="txtblk" Width="300" Height="30" Foreground="Red"></TextBlock>
            <Button Background="BurlyWood" Content="Load items in file" Height="30" Width="120" Click="Button_Click" Grid.Row="1"></Button>
        </Grid>
    </Window>
    

    Have keyup event and check whether the combination of keys are pressed or not, then write your logic.

    here when you press ctrl+l, text will be displayed.


    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Monday, August 5, 2013 6:53 AM
  • You could use a RoutedUICommand and an InputBinding:


    XAML:

    <UserControl.Resources>
     <RoutedUICommand x:Key="ClickCommand" Text="Text" />
    </UserControl.Resources>
    <UserControl.CommandBindings>
        <CommandBinding Command="{StaticResource ClickCommand}" 
                        Executed="ClickHandler" />
    </UserControl.CommandBindings>
    <UserControl.InputBindings>
        <KeyBinding Key="C" Modifiers="Ctrl" Command="{StaticResource ClickCommand}" />
    </UserControl.InputBindings>
    <Grid>
        <Button Content="Click here" Command="{StaticResource ClickCommand}"/>
    </Grid>

    C#:

    private void ClickHandler(object sender, RoutedEventArgs e) {
          //handle click event
        }

    Monday, August 5, 2013 7:20 AM
  • Its working in windows form(wpf window) but in user control its not working at first time..if i need to clicked any button in user control  after that only the shortcut key working..

    In my project having one main window another one user control..in main window have expander in left hand side and right side content control..i clicked expander button it will show the user control in content control..that user control contain addproject button...now i want set shortcut key for that..i set your coding but i will not working at first time ...first time i want to click addproject button through mouse...next time only that shortcut is working...Please help me

    main window coding..

    Xaml...

    <Window x:Class="Wpf.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:viewer="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"
            Title="Window1" Height="300" Width="390" >
        <Grid>
            <Expander Header="expander1" Height="81" HorizontalAlignment="Left" Name="expander1" VerticalAlignment="Top" Width="135">
                <Grid>
                    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="22,17,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
                </Grid>
            </Expander>
            <ContentControl Margin="141,0,0,0" Name="contentControl1"></ContentControl>
        </Grid>

    </Window>

    Backend....

    namespace Wpf
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            

            public Window1()
            {
                InitializeComponent();
                
                
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                contentControl1.Content = new UserControl1();
            }
            //private bool _isReportViewerLoaded;
            
        }
    }

     User Control coding..

    XAML...

    <UserControl x:Class="Wpf.UserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 mc:Ignorable="d"
                 d:DesignHeight="300" d:DesignWidth="300" KeyUp="Window_KeyUp">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="200"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
            <TextBlock x:Name="txtblk" Width="300" Height="30" Foreground="Red"></TextBlock>
            <Button Background="BurlyWood" Content="Load items in file" Height="30" Width="120"  Grid.Row="1"></Button>
        </Grid>
    </UserControl>

    Backend..

    namespace Wpf
    {
        /// <summary>
        /// Interaction logic for UserControl1.xaml
        /// </summary>
        public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
            }

            private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
            {
                if (Key.L.Equals(e.Key) && Keyboard.Modifiers.Equals(ModifierKeys.Control))
                {
                    txtblk.Text = "Data Saved Successfully";
                }
            }
        }
    }

    • Edited by Nandakumar A Monday, August 5, 2013 9:36 AM Insert coding
    Monday, August 5, 2013 7:35 AM