Handling WPF Routed Events with MVVM Commands - EASY WPF (.NET Core)

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024
  • Learn how to handle any WPF control routed event with a command in order to decouple views from view models.
    Other topics covered include dependency properties and implementing commands.
    SOURCE CODE: github.com/Sin...

ความคิดเห็น • 37

  • @willgordon5737
    @willgordon5737 หลายเดือนก่อน

    Oh boy MVVM has its learning curve for sure, I every now and then look at C++ or assembly code of some open source software. To get reminded how easy C# is

  • @olivin8937
    @olivin8937 4 หลายเดือนก่อน

    Thanks for making this video! I had an issue with routed events not being consumed properly in the XamlBehaviors package and events were bubbling up to where they shouldn't be. Getting rid of that seemingly dysfunctional Microsoft package and adopting the approach in this video took care of my problem.

  • @ricardo7cordas
    @ricardo7cordas 6 หลายเดือนก่อน +1

    pretty straight forward and clear explanation. many thanks

  • @user-mj1ms6jb3z
    @user-mj1ms6jb3z 2 ปีที่แล้ว

    I find it in many website pages, now in your vedio, I got it which I real need. VERY HAPPY!!! THANK YOU VREY MUCH!

  • @rahulmathew8713
    @rahulmathew8713 3 ปีที่แล้ว

    You solved my dilemma, my view model was making that API Call, thank you for showing me the path to fix that.

  • @bartomiejciurla8472
    @bartomiejciurla8472 4 หลายเดือนก่อน

    It helped a lot! Saved for the future

  • @user-mj1ms6jb3z
    @user-mj1ms6jb3z 2 ปีที่แล้ว +1

    dependency properties not only data, but also command.every thing is class, oh, new world.😊

  • @langgo8927
    @langgo8927 3 ปีที่แล้ว +1

    Great tutorial and explanation! I've never seen Thanks for your lecture!

    • @SingletonSean
      @SingletonSean  3 ปีที่แล้ว +1

      Thanks lang go, much appreciated!

  • @precastdetailing
    @precastdetailing 24 วันที่ผ่านมา

    Why do you need Dependency Properties for your command (as opposed to normal properties) that you can bind to?

  • @VinuP2023
    @VinuP2023 4 ปีที่แล้ว

    Thanks much for the video. Pls keep doing more of this 🙏

    • @SingletonSean
      @SingletonSean  4 ปีที่แล้ว +1

      No problem Vinay, more on the way!

  • @solvedplus858
    @solvedplus858 ปีที่แล้ว

    Awesome
    this is nery awesome
    many thanks for your effort

  • @User_Masuri
    @User_Masuri 2 ปีที่แล้ว +1

    Wow! I've learned so much things from your video! you taught me what I've been doing wrong! Thank you for making good video!

  • @mcnets
    @mcnets 3 ปีที่แล้ว

    Really great. Thanks a lot for your time!

  • @zenobiusztasak8604
    @zenobiusztasak8604 3 ปีที่แล้ว +1

    Hi I have one question. You said you have to separate view model from the view but in TodoList.xaml you have got 'IsCompleted' property which comes from viewModel(TodoItem). Is there a way to some how bind it outside like commands? Thanks
    EDIT: TodoItem is not a viewmodel it does not implement INotifyPropertyChanged(I checked in your git repo). But it must be if for instance I would like to change 'IsCompleted' property. So the question still remains :)
    In other word, this view is not bound to specific viewmodel but it is bound to specific viewmodel properties, is it posibble
    to avoid that also or it is bad thinking ? Thanks a lot.

    • @SingletonSean
      @SingletonSean  3 ปีที่แล้ว

      Hey Zenobiusz, this is bad thinking on my part. TodoItem should definitely be replaced with a TodoItemViewModel that implements INotifyPropertyChanged. Binding to objects that do not implement INotifyPropertyChanged will result in memory leaks. That said, if you would want a command on the todo listing, I'd recommend implementing the command in the TodoItemViewModel. Hopefully this sparks some ideas, and thanks for the analysis!

  • @daedalusrasmus3278
    @daedalusrasmus3278 4 ปีที่แล้ว +1

    Cool! Can you show us a simple way to communicate between 2 UserControls? Say UserControl#1 has a Button, and UserControl#2 has a TextBox... Then when UserControl#1's Button gets clicked, UserControl#2's TextBox changes its text to UserControl#1's Content, or have its Background change to the Button's Background/Foreground color or something like that. Or maybe something similar to your 'Switching Between Multiple Views' video but instead of hard coding the Controls in the MainWindow, have them each in separate UserControls. Does that involve the RelativeSource as well? I've been trying DelegateCommands/RelayCommands but I can't seem to make them work. I'm trying to understand as much as I can and do everything manually before I try using framework :)

    • @nohopestage
      @nohopestage 2 ปีที่แล้ว +1

      It's been a year but did you get it working?

  • @phil1105
    @phil1105 ปีที่แล้ว

    Hello, how can you do that without using usercontrols? I'm not using any unsercontrol in my app, but I still want to achieve this. Actually, I want to use the loaded event off the window. Since I'm not like "calling" the window control in XMAL I cannot bind the dependency property to a command. I could, like DIBYENDU SAHA said, use the interaction namespace, but this adds a DLL to my project for using only one feature. If I could bind the loaded event of the window to a command it would be better that using the interaction namespace. I could have only one exe file instead of having the exe and the dll. Thank you.

  • @ivandrofly
    @ivandrofly 2 ปีที่แล้ว

    I like your channel name!!!

  • @feitan8745
    @feitan8745 4 ปีที่แล้ว

    Thanks again for the content. Did you ever consider start using FoodyWeavers?

    • @SingletonSean
      @SingletonSean  4 ปีที่แล้ว

      Hey Feitan, I've actually never heard of it before. I did some research on weaving and it seems like an interesting topic. I read an article about how it can be used to weave in INotifyPropertyChanged automatically, which is pretty neat. I could probably find a few use cases for it. Thanks for the suggestion!

    • @feitan8745
      @feitan8745 4 ปีที่แล้ว

      @@SingletonSean Actually i was about to ask you in the last video, but somehow i forgot, or maybe i thought you already tried and just didn't like, i've been using it for a while now, and it's great, also helps cleaning the code.

  • @shahjahan12003
    @shahjahan12003 4 ปีที่แล้ว

    Execellent.

  • @ivandrofly
    @ivandrofly 2 ปีที่แล้ว

    Thanks

  • @rahulmathew8713
    @rahulmathew8713 3 ปีที่แล้ว

    Awesome

  • @dibyendusaha1295
    @dibyendusaha1295 3 ปีที่แล้ว +1

    Its a nice video.but can't we achieve the same by invoking interaction namespace and then calling invokecommandaction under the interaction. Something Like this :

    • @SingletonSean
      @SingletonSean  3 ปีที่แล้ว

      Hey Dibyendu, if it works it works!

  • @Touseefelahi07
    @Touseefelahi07 4 ปีที่แล้ว

    Thank you very much for this awesome concept.
    Can you please push the code to Github?

    • @SingletonSean
      @SingletonSean  4 ปีที่แล้ว

      Thanks for watching Touseef. Here's a link to the source code: github.com/SingletonSean/wpf-event-commands

  • @EngDuraid
    @EngDuraid 4 ปีที่แล้ว

    Thank you but how can i bind a command which is found in parent view not in child view

    • @SingletonSean
      @SingletonSean  4 ปีที่แล้ว +1

      Hey Duraid, I think a potential solution would be using RelativeSource, specifically FindAncester mode:
      www.c-sharpcorner.com/UploadFile/yougerthen/relativesources-in-wpf/
      However, for an MVVM application, I would recommend handling this parent-child communication in the view model. I'm interested in demoing this in a future video. Great question!

    • @EngDuraid
      @EngDuraid 4 ปีที่แล้ว

      @@SingletonSean Thank you very much, i'll be waiting this demo with love :)

  • @AP-pz9wp
    @AP-pz9wp 2 ปีที่แล้ว +1

    MVVM architecture is a bunch extra code to do the simplest things ever.

  • @gendir9827
    @gendir9827 2 ปีที่แล้ว

    Isn't it enough to use propdp snippet in View's code-behind(xaml.cs , which is a UserControl in my case, following your MVVM switching tutorial with currentviewmodel variable) just for being able to resolve the usage of that dp in xaml(view) file? I do just as it's done in this video, but my IDE can't resolve dependency property inside "TreeView" tag in xaml file....??? what is wrong??
    by propd I mean snippet : "public ICommand DragCommand
    {
    get { return (ICommand)GetValue(DragCommandProperty); }
    set { SetValue(DragCommandProperty, value); }
    }
    public static readonly DependencyProperty DragCommandProperty =
    DependencyProperty.Register("DragCommand", typeof(ICommand), typeof(FormProjectWorksView), new PropertyMetadata(null));
    PS:"formprojectworksview" is the view class' name in the code behind, inside of which this dp is defined .
    so when I type in xaml There is no such property error is fired.