Intersoft ClientUI 8 > ClientUI Controls > Control Library > Button Controls Overview > UXDropDownButton > How-to: Use Dropdown Button to Provide Selection of Commands |
This example shows how to create menu for selection in UXDropDownButton using UXMenuItem and create DelegateCommand in the ViewModel and bind it to UXMenuItem's Command in the View.
The following example shows the EditorViewModel with NewAction and SaveAction DelegateCommand.
In the sample, the UXDropDownButton has two UXMenuItem, New and Save. The New UXMenuItem bind to NewAction DelegateCommand and Save UXMenuItem bind to SaveAction DelegateCommand.
To learn more about how to use MVVM pattern using ClientUI, see Walkthrough: Create Your First ClientUI Application using MVVM Pattern. |
Code
XAML |
Copy Code
|
---|---|
<Intersoft:UXPage.Resources> <ViewModels:EditorViewModel x:Key="EditorViewModel"></ViewModels:EditorViewModel> </Intersoft:UXPage.Resources> <Grid x:Name="LayoutRoot" DataContext="{StaticResource EditorViewModel}"> <Intersoft:UXDropDownButton Content="File" HorizontalAlignment="Left" Margin="10,10,0,0" Name="uXDropDownButton1" VerticalAlignment="Top"> <Intersoft:UXMenuItem Header="New" Command="{Binding NewAction}"/> <Intersoft:UXMenuItem Header="Save" Command="{Binding SaveAction}"/> </Intersoft:UXDropDownButton> </Grid> |
C# |
Copy Code
|
---|---|
public class EditorViewModel : ViewModelBase { public EditorViewModel() { this.NewAction = new DelegateCommand(NewCommandAction, CanNewAction); this.SaveAction = new DelegateCommand(SaveCommandAction, CanSaveAction); } public DelegateCommand NewAction { get; private set; } public DelegateCommand SaveAction { get; private set; } private bool CanNewAction(object parameter) { //this method validate whether or not the New UXMenuItem is active //In this sample, I will keep the New UXMenuItem to be always active return true; } private bool CanSaveAction(object parameter) { //this method validate whether or not the Save UXMenuItem is active //In this sample, I will keep the Save UXMenuItem to be always active return true; } private void NewCommandAction(object parameter) { //this method will be triggered when the New UXMenuItem is clicked MessageBoxServiceProvider.Standalone = true; MessageBoxServiceProvider.Show("Execute New Action", "Information", Intersoft.Client.UI.Aqua.UXDesktop.MessageBoxButton.OK, Intersoft.Client.UI.Aqua.UXDesktop.MessageBoxImage.Information, null); } private void SaveCommandAction(object parameter) { //this method will be triggered when the Save UXMenuItem is clicked MessageBoxServiceProvider.Show("Execute Save Action", "Information", Intersoft.Client.UI.Aqua.UXDesktop.MessageBoxButton.OK, Intersoft.Client.UI.Aqua.UXDesktop.MessageBoxImage.Information, null); } } |