PDA

View Full Version : سوال: رویداد KeyDown در MVVM



SokooteShab1
یک شنبه 01 دی 1392, 10:32 صبح
سلام دوستان
من برای استفاده از رویداد KeyDown در mvvm از کد زیر استفاده کردم ولی نمی دونم چه جوری باید از پارامتر Sender استفاده کنم.


RelayCommand<object, KeyEventArgs> _KeyDown;
public ICommand KeyDownCommand
{
get
{
if (_KeyDown == null)
{
_KeyDown = new RelayCommand<object, KeyEventArgs>(KeyDowmExecute);
}
return _KeyDown;
}
}

private void KeyDowmExecute( object sender,KeyEventArgs e)
{
if (_keydown)
{
if (e.Key == Key.Enter & (sender as TextBox).AcceptsReturn == false)
MoveToNextUIElement(e);
}
}

}
اگه راهنماییم کنید ممنون میشم.

SokooteShab1
یک شنبه 01 دی 1392, 12:16 عصر
موقع اجرا به این خط که میرسه:

if (e.Key == Key.Enter & (sender as TextBox).AcceptsReturn == false)
این error رو میده.
Object reference not set to an instance of an object.
در واقع می خوام با زدن اینتر فوکوس بره به کنترل بعدی. البته از طریق Mvvm
خواهش میکنم کمک کنید.

SokooteShab1
سه شنبه 03 دی 1392, 07:22 صبح
یعنی از اساتید هیچ کی تاحالا به این مورد برخورد نداشته................. یا کار نکرده..!!!!!!!!!!!؟؟:متعجب::متعجب ::متعجب:

امید خطیبی
سه شنبه 03 دی 1392, 07:26 صبح
سلام دوست عزیز
آخه من دقیق نمی دونم کارت چیه از نوشتن این کد تا بهت بگم که چجوری می تونی این کار رو انجام بدی
Enter رو واسه چه کاری می خواهی بنویسی که چی کار بکنه
موفق باشی

SokooteShab1
سه شنبه 03 دی 1392, 09:42 صبح
می خوام وقتی کاربر کلید اینتر رو زد فوکوس بره به کنترل بعدی البته از طریق mvvm

امید خطیبی
سه شنبه 03 دی 1392, 17:50 عصر
با سلام

دوست عزیز ببین از این نمونه کد می تونی استفاده کنی

داخل App.xaml



EventManager.RegisterClassHandler(typeof(TextBox), TextBox.KeyDownEvent, new KeyEventHandler(TextBox_KeyDown)); EventManager.RegisterClassHandler(typeof(CheckBox) , CheckBox.KeyDownEvent, new KeyEventHandler(CheckBox_KeyDown));

این رو وارد کن

این قسمت هم برای CodeBehind می باشد



void TextBox_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter & (sender as TextBox).AcceptsReturn == false) MoveToNextUIElement(e); } void CheckBox_KeyDown(object sender, KeyEventArgs e) { MoveToNextUIElement(e); //Sucessfully moved on and marked key as handled. //Toggle check box since the key was handled and //the checkbox will never receive it. if (e.Handled == true) { CheckBox cb = (CheckBox)sender; cb.IsChecked = !cb.IsChecked; } } void MoveToNextUIElement(KeyEventArgs e) { // Creating a FocusNavigationDirection object and setting it to a // local field that contains the direction selected. FocusNavigationDirection focusDirection = FocusNavigationDirection.Next; // MoveFocus takes a TraveralReqest as its argument. TraversalRequest request = new TraversalRequest(focusDirection); // Gets the element with keyboard focus. UIElement elementWithFocus = Keyboard.FocusedElement as UIElement; // Change keyboard focus. if (elementWithFocus != null) { if (elementWithFocus.MoveFocus(request)) e.Handled = true; } }


این یک مدلش بود

و این هم مدلی دیگر



<TextBox Width="100" Text="{Binding Name, Mode=TwoWay}" UI:FocusAdvancement.AdvancesByEnterKey="True" />


این رو هم داخل کلاسی که می خواهید مخصوص به فکوس های شما باشد



public static class FocusAdvancement { public static bool GetAdvancesByEnterKey(DependencyObject obj) { return (bool)obj.GetValue(AdvancesByEnterKeyProperty); } public static void SetAdvancesByEnterKey(DependencyObject obj, bool value) { obj.SetValue(AdvancesByEnterKeyProperty, value); } public static readonly DependencyProperty AdvancesByEnterKeyProperty = DependencyProperty.RegisterAttached("AdvancesByEnterKey", typeof(bool), typeof(FocusAdvancement), new UIPropertyMetadata(OnAdvancesByEnterKeyPropertyCha nged)); static void OnAdvancesByEnterKeyPropertyChanged(DependencyObje ct d, DependencyPropertyChangedEventArgs e) { var element = d as UIElement; if(element == null) return; if ((bool)e.NewValue) element.KeyDown += Keydown; else element.KeyDown -= Keydown; } static void Keydown(object sender, KeyEventArgs e) { if(!e.Key.Equals(Key.Enter)) return; var element = sender as UIElement; if(element != null) element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } }


موفق باشید. ( امیدوارم کمکی کرده باشم)