Binding with commans and command parameter in MVVM

I desire to use a dynamic list of items as in the Data Binding example in the control documentation.

How can I attach a command  with a command parameter to each Item in the ItemsSource collection where the command is bound to the viewmodel and the command parameter is the DisplayMemberPath value?

4 Replies 1 reply marked as answer

MS Mugundhan Saravanan Syncfusion Team January 25, 2021 11:53 AM UTC

Hi Arlyn,

Greetings from
Syncfusion.

Query
: Binding with command and command parameter in MVVM

We have checked the reported query, we don’t bind the command and we can’t get the command parameter value as the selected item to the ItemSource collections directly. We suggest you use the SelectedItem property to get the value of which is selected from the ItemsSource collection in the ViewModel. We have created a workaround sample for reference, please check the sample and let us know any concern.


Code Snippet:

 
<editors:SfTextBoxExt HorizontalAlignment="Center"  
                              VerticalAlignment="Center"  
                              Width="300" 
                              Height="40" 
                              SearchItemPath="Name" 
                              SelectedItem="{Binding SelectedItems,Mode=TwoWay}" 
                              AutoCompleteMode="Suggest" 
                              AutoCompleteSource="{Binding Employees}">

ViewModel.cs

    private object selectedItem;
 
 
        public event PropertyChangedEventHandler PropertyChanged; 
        private void OnPropertyRaised(string propertyname) 
        { 
            if (PropertyChanged != null) 
            { 
                PropertyChanged(this, new PropertyChangedEventArgs(propertyname)); 
            } 
        } 
 
        public object SelectedItems 
        { 
            get { return selectedItem; } 
            set 
            { 
                selectedItem = value; 
                OnPropertyRaised("SelectedItem"); 
                ItemChanged(); 
            } 
        }

     private void ItemChanged()
 
        { 
            var item = (this.SelectedItems as Employee).Name.ToString(); 
            MessageBox.Show("SelectedItem is " +item); 
             
        } 
 

Sample:  https://www.syncfusion.com/downloads/support/forum/161810/ze/AutoCompleteMVVMWPF-966253085

Regards,
Mugundhan S.




AL Arlyn Liddell January 25, 2021 08:20 PM UTC

I am sorry that I was not clear in my quetsion.  The issue is with the SfNavigationDrawer.  I want to attach a command and command parameter to each NavigationItem using the same command and the unique value used as the display member as the command parameter.

I also am unable to bind to the SelectedItem.  The attached file when executed, the selection variable is always null when the ItemClicked fires.

Attachment: NavigationDrawerTest_4c8abb9c.zip


AS Anandraj Selvam Syncfusion Team January 27, 2021 01:45 AM UTC

Hi Arlyn Liddell, 

Greetings from Syncfusion. 

We are currently validating on this and we will update the details on January 28, 2021. We appreciate your patience until then. 

Regards, 
Anand Raj S. 



SS Suganya Sethuraman Syncfusion Team January 28, 2021 10:00 AM UTC

Hi Arlyn,

We have analyzed your requirement. We have prepared the sample by using Command binding in NavigationItem of SfNavigationDrawer. Please have a sample for your reference,

Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/NavigationDrawerCommand-1383559996

Please let us know if you have any concerns.

Regards,
Suganya Sethuraman.
 


Marked as answer
Loader.
Up arrow icon