PDA

View Full Version : چجوري ميشه يه shortcut رو به يه دكمه نسبت داد؟!!!



amateur.programmer
سه شنبه 13 اردیبهشت 1390, 15:42 عصر
69497سلام دوستان
من ميدونم كه چطور shortcut رو از صفحه كليد بخونم ولي نميدونم چجوري اونو به يه button نسبت بدم؟ يا لااقل كاري كنم كه همون كار button رو انجام بده؟!!
يه نمونه برنامه كوچيك ضميمه كردم ميتونيد ببينيد و راهنماييم كنيد!!!
ممنون ميشم

مهدی فرزاد
سه شنبه 13 اردیبهشت 1390, 21:08 عصر
سلام
مثال زیر رو ببینید کد ها کاملا ساده و گویاست
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">

<Window.Resources>
<local:HelloWorldCommand x:Key="commandUserInputKey" />
</Window.Resources>

<Window.InputBindings>
<KeyBinding Key="O" Modifiers="Control" Command="{StaticResource commandUserInputKey}" />
</Window.InputBindings>

<Grid>
<Button Content="Button" Height="33" Name="button1" Width="126" Command="{StaticResource commandUserInputKey}" />
</Grid>
</Window>

public class HelloWorldCommand : ICommand
{

public void Execute(object parameter)
{
MessageBox.Show("Hello, world");
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
}


لینک زیر رو هم مطالعه کنید
msdn.microsoft.com/en-us/library/ms752308.aspx#Y2376

مهدی فرزاد
سه شنبه 13 اردیبهشت 1390, 21:46 عصر
مثال شما هم به شکل زیر باید اصلاح بشه
<Window.InputBindings>
<KeyBinding Gesture="Control+N" CommandParameter="Ctrl + n"
Command ="Help" />
<KeyBinding Gesture="Control+O" CommandParameter="Ctrl + o"
Command ="Help" />
</Window.InputBindings>
<Window.CommandBindings>
<CommandBinding Command="Help"
CanExecute="HelpCanExecute" Executed="HelpExecuted" />
</Window.CommandBindings>
<Grid>

<Button Content="Button1" Height="23" HorizontalAlignment="Left" Margin="234,127,0,0" Name="button1" VerticalAlignment="Top" Width="75" CommandParameter="Ctrl + n" Command="Help"/>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="125,51,0,0" Name="textBlock1" Text="" VerticalAlignment="Top" Width="149" />
<Button Content="Button2" Height="23" HorizontalAlignment="Left" Margin="96,127,0,0" Name="button2" VerticalAlignment="Top" Width="75" CommandParameter="Ctrl + o" Command="Help"/>
</Grid>


public partial class MainWindow : Window
{
public MainWindow()
{
// InitializeComponent();
}
void HelpCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}

void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
{

switch (e.Parameter.ToString())
{
case "Ctrl + n":
textBlock1.Text = "button1 click";

break;
case "Ctrl + o":
textBlock1.Text = "button2 click";
break;
}
}
}