jascam / CodePlexFoo

0 stars 0 forks source link

Request: WPF Keybinding to close the current view from ViewModel without code behind #165

Open jascam opened 6 years ago

jascam commented 6 years ago

A lot of programmers and demo makers seem to forget that the keyboard is still an efficient form of data entry and UI control. A sample to something a little more complex like pressing the escape key to handle multiple states in a WPF application would be desirable. The states on each key press would be undo the current field, undo the current record, close the view without code behind.

Migrated CodePlex Work Item Details

CodePlex Work Item ID: '7527' Vote count: '1'

jascam commented 6 years ago

Hello N2Cheval

Thank you for using our sample request service! (http://1code.codeplex.com/wikipage?title=NEW%2c%20FREE%20Code%20Sample%20Request%20Service%20from%20Microsoft%20All-In-One%20Code%20Framework)

From your description, I learnt that you want a code sample that handles the key press by KeyBinding on the close command, and implement the close command using the MVVM pattern. It would need code-behind to implement the close command, but regarding the KeyBinding, it does not need code-behind.

Is I understanding correct?

Thanks Jialiang Ge

This comment was posted by Jialiang on 11/1/2010

jascam commented 6 years ago

That's right, but a bit more. Re-reading my question, unfortunately I think I'm asking two things; 1) Close current view from ViewModel cleanly without code behind in the View and 2) a cache-able state machine to roll back changes. For example with #1 the best so far has three parts, but I think there must be a better way of doing it. XAML

ViewModel RelayCommand _closeCommand; public ICommand CloseCommand { get { if (_closeCommand == null){ _closeCommand = new RelayCommand(param => Close(),param => CanClose()); } return _closeCommand; } } public event Action RequestClose; public virtual void Close(){ if (RequestClose != null) { RequestClose(); } } public virtual bool CanClose() { return true; } View code behind private SomeThingViewModel _vm; public SomeThingView() { Mouse.OverrideCursor = Cursors.Wait; InitializeComponent(); this.Loaded += SomeThingView_Loaded; Mouse.OverrideCursor = null; } private void SomeThingView_Loaded(object sender, RoutedEventArgs e) { _vm = (SomeThingViewModel)this.DataContext; _vm.RequestClose += () => { Close(); }; } For #2, I've not seen a good way of doing this with #1 as the final result. Can this be done? This comment was posted by N2Cheval on 11/1/2010
jascam commented 6 years ago

This comment was posted by on 2/21/2013