Search code examples
c#wpfbindingcommandparameter

how to use object class instance recived as command parameters in WPF


I am passing command parameters to a command. and receiving it like this

 public void SelectTestCase(object Dev)
    {
        try
        {               
            _navigationStore.CurrentViewModel = new TestCaseViewModel(_navigationStore);                
        }
        catch (Exception e)
        {
        }
    }

in this Object Dev will be carying data related to Device. but if i do dev.DeviceName this is giving error because dev object is recieving data on runtime. how can i use this Dev object and get data on runtime


Solution

  • You have the parameter specifically as a generic "object" rather than the specific class type. You need to type-cast it. Ex:

    public void SelectTestCase( object Dev )
    {
       if( Dev is myDeviceTypeClass )
       {
          var tmp = (myDeviceClass)Dev;
          // Now you can use as needed
          MessageBox.Show( tmp.DeviceName );
       }
       
       // if you have different POSSIBLE device classes passed in,
       // just test for those too.
    }