Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Passing two parameters in Command biding in WPF and MVVM

Passing two parameters in Command biding in WPF and MVVM

Scheduled Pinned Locked Moved WPF
questionwpfcsharparchitecturehelp
11 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    indian143
    wrote on last edited by
    #1

    Hi Friends, I have two Textboxes, One for Id and another for the Name, I need to pass both of these two into MyViewModel class Id and Name Properties in a Button Click, so far I am able to pass one of them to the ViewModel, how can I pass both the parameters to the ViewModel, any help is greatly helpful. Thanks in advance please. So far I am able to do as below in View

    In the Command class

    public void Execute(object parameter)
    {
    var values = (string)parameter;

            this.ViewModel.AddAnItem(values);
        }
    

    In View Model

        public void AddAnItem(string \_name)
        {
            int t;
    
            if ((\_personNames == null) || (\_personNames.Count() <= 0))
                t = 1;
            else
                t = (\_personNames.Count <= 0) ? 1 : \_personNames.Max(x => x.Id) + 1;
            \_personNames.Add(new TestData(t, \_name));
        }        
    

    I want to change this AddAnItem to take two parameters int and string and if possible return value also, but return is not important, what is important is to take two parameters. Please need help, any sort of help a link or code snippet or even a suggestion also helps.

    Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

    P 1 Reply Last reply
    0
    • I indian143

      Hi Friends, I have two Textboxes, One for Id and another for the Name, I need to pass both of these two into MyViewModel class Id and Name Properties in a Button Click, so far I am able to pass one of them to the ViewModel, how can I pass both the parameters to the ViewModel, any help is greatly helpful. Thanks in advance please. So far I am able to do as below in View

      In the Command class

      public void Execute(object parameter)
      {
      var values = (string)parameter;

              this.ViewModel.AddAnItem(values);
          }
      

      In View Model

          public void AddAnItem(string \_name)
          {
              int t;
      
              if ((\_personNames == null) || (\_personNames.Count() <= 0))
                  t = 1;
              else
                  t = (\_personNames.Count <= 0) ? 1 : \_personNames.Max(x => x.Id) + 1;
              \_personNames.Add(new TestData(t, \_name));
          }        
      

      I want to change this AddAnItem to take two parameters int and string and if possible return value also, but return is not important, what is important is to take two parameters. Please need help, any sort of help a link or code snippet or even a suggestion also helps.

      Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Why? Just put two string properties in your ViewModel and binding your Text boxes to them. You can then read them from your command.

      This space for rent

      I 1 Reply Last reply
      0
      • P Pete OHanlon

        Why? Just put two string properties in your ViewModel and binding your Text boxes to them. You can then read them from your command.

        This space for rent

        I Offline
        I Offline
        indian143
        wrote on last edited by
        #3

        The condition in the company is to bind them from xaml but when I am trying to bind the ViewModel from xaml, I am getting value only if I bind one but when I am trying to bind the second one and try to raise NotifyPropertyChanged event then both the values are not coming inth xaml. Then I tried add static resource of ViewModel in xaml, the ViewModel has the same namespace and assembly as xaml view file, its not letting me add the static resource of the ViewModel. Without ViewModel is not being accessed as StaticResource I am not able to reference the ViewModels Properties in the View, its giving me compile error saying that ViewModel is not available in the namespace. I tried it many ways not not helping me out, I wrote the code as above question. Anyhelp please.

        Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

        P 1 Reply Last reply
        0
        • I indian143

          The condition in the company is to bind them from xaml but when I am trying to bind the ViewModel from xaml, I am getting value only if I bind one but when I am trying to bind the second one and try to raise NotifyPropertyChanged event then both the values are not coming inth xaml. Then I tried add static resource of ViewModel in xaml, the ViewModel has the same namespace and assembly as xaml view file, its not letting me add the static resource of the ViewModel. Without ViewModel is not being accessed as StaticResource I am not able to reference the ViewModels Properties in the View, its giving me compile error saying that ViewModel is not available in the namespace. I tried it many ways not not helping me out, I wrote the code as above question. Anyhelp please.

          Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Without you showing your ViewModel and View here, it's impossible to say why it doesn't work, however I would take a guess that you aren't actually referencing the namespace in your XAML. Typically, I would expect to see something like this in your Window/UserControl header:

          xmlns:vm="clr-namespace:MyApplication.ViewModelNamespace"

          Then you would reference this as such

          <Window.DataContext>vm:NameOfViewModel</Window.Resources>

          This space for rent

          I 1 Reply Last reply
          0
          • P Pete OHanlon

            Without you showing your ViewModel and View here, it's impossible to say why it doesn't work, however I would take a guess that you aren't actually referencing the namespace in your XAML. Typically, I would expect to see something like this in your Window/UserControl header:

            xmlns:vm="clr-namespace:MyApplication.ViewModelNamespace"

            Then you would reference this as such

            <Window.DataContext>vm:NameOfViewModel</Window.Resources>

            This space for rent

            I Offline
            I Offline
            indian143
            wrote on last edited by
            #5

            Hi, My here is my View

            And ViewModel

            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;
            using System.Windows.Input;

            namespace TestWPFApplication
            {
            public class TestViewModel : INotifyPropertyChanged
            {
            TestData _testData;

                //public TestCommand TestCommand { get; set; }
                public TestSpecificCommand TestSpecificCom
            
            P 1 Reply Last reply
            0
            • I indian143

              Hi, My here is my View

              And ViewModel

              using System.Linq;
              using System.Text;
              using System.Threading.Tasks;
              using System.Windows.Input;

              namespace TestWPFApplication
              {
              public class TestViewModel : INotifyPropertyChanged
              {
              TestData _testData;

                  //public TestCommand TestCommand { get; set; }
                  public TestSpecificCommand TestSpecificCom
              
              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              TestViewModel is not a resource, yet you are attempting to use it as though it is. Change your Window.Resources section to a Window.DataContext that looks like this

              <Window.DataContext>
              <testns:TestViewModel />
              </Window.DataContext>

              This space for rent

              I 1 Reply Last reply
              0
              • P Pete OHanlon

                TestViewModel is not a resource, yet you are attempting to use it as though it is. Change your Window.Resources section to a Window.DataContext that looks like this

                <Window.DataContext>
                <testns:TestViewModel />
                </Window.DataContext>

                This space for rent

                I Offline
                I Offline
                indian143
                wrote on last edited by
                #7

                Hi, I changed it as below, but still it is giving me the same error, any help please, still it is saying me that and in the UI it is saying invalid xaml.

                Error 1 The name "TestViewModel" does not exist in the namespace "clr-namespace:TestWPFApplication.ViewModel". \\VBOXSVR\VMShared\Test Projects\TestWPFApplication\TestWPFApplication\View\MainWindow.xaml 9 9 TestWPFApplication

                My code is below, any help please?

                Now I changed a little bit the ViewModel I put in a folder called ViewModel and Model in Model folder and changed their namespaces and changed the reference of clr-namespace as well but still the error persists.

                Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

                P M 2 Replies Last reply
                0
                • I indian143

                  Hi, I changed it as below, but still it is giving me the same error, any help please, still it is saying me that and in the UI it is saying invalid xaml.

                  Error 1 The name "TestViewModel" does not exist in the namespace "clr-namespace:TestWPFApplication.ViewModel". \\VBOXSVR\VMShared\Test Projects\TestWPFApplication\TestWPFApplication\View\MainWindow.xaml 9 9 TestWPFApplication

                  My code is below, any help please?

                  Now I changed a little bit the ViewModel I put in a folder called ViewModel and Model in Model folder and changed their namespaces and changed the reference of clr-namespace as well but still the error persists.

                  Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  And that would be correct. Look at the code you posted. The ViewModel isn't in that namespace, it's in TestWPFApplication. The error was very explicit about what the problem was.

                  This space for rent

                  I 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    And that would be correct. Look at the code you posted. The ViewModel isn't in that namespace, it's in TestWPFApplication. The error was very explicit about what the problem was.

                    This space for rent

                    I Offline
                    I Offline
                    indian143
                    wrote on last edited by
                    #9

                    Thanks for all your help but I found the Problem that I was struggling with, it was because of the network drive I loading the Application path from. When we are using loading the static resources data context the UI can take them only if they from trusted source, my application was on network drive. Thanks for all your support Pete and Rick, without you guys I couldn't have got this far. About my wrong code here I was changing the code all the time, may be pasted it when it was wrong. Thank you very much for that.

                    Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

                    P 1 Reply Last reply
                    0
                    • I indian143

                      Thanks for all your help but I found the Problem that I was struggling with, it was because of the network drive I loading the Application path from. When we are using loading the static resources data context the UI can take them only if they from trusted source, my application was on network drive. Thanks for all your support Pete and Rick, without you guys I couldn't have got this far. About my wrong code here I was changing the code all the time, may be pasted it when it was wrong. Thank you very much for that.

                      Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #10

                      Glad you got there in the end.

                      This space for rent

                      1 Reply Last reply
                      0
                      • I indian143

                        Hi, I changed it as below, but still it is giving me the same error, any help please, still it is saying me that and in the UI it is saying invalid xaml.

                        Error 1 The name "TestViewModel" does not exist in the namespace "clr-namespace:TestWPFApplication.ViewModel". \\VBOXSVR\VMShared\Test Projects\TestWPFApplication\TestWPFApplication\View\MainWindow.xaml 9 9 TestWPFApplication

                        My code is below, any help please?

                        Now I changed a little bit the ViewModel I put in a folder called ViewModel and Model in Model folder and changed their namespaces and changed the reference of clr-namespace as well but still the error persists.

                        Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

                        M Offline
                        M Offline
                        Meshack Musundi
                        wrote on last edited by
                        #11

                        Your binding mode should be OneWayToSource not OneWay,

                        <TextBox Text="{Binding Name, Mode=OneWayToSource}" HorizontalAlignment="Left" Height="22" Margin="160,64,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>

                        Or TwoWay if you want your View and the property in your ViewModel to modify each other.

                        "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups