.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

WPF Custom RoutedUI Commands

Posted By:Diptimaya Patra       Posted Date: May 31, 2010    Points: 25    Category: WPF    URL: http://www.dotnetspark.com  

WPF Custom RoutedUI Commands. In this article we will see how we can use Custom RoutedUI Commands in WPF Application.
 

Creating A WPF Project

Fire up Visual Studio 2008, create a WPF Application, and name it as CustomCommandRoutedUI.

image1.gif

Now let's create a static class in Window1.xaml.cs

image2.gif

Code view of the above example
    #region MyCustomCommand
    public static class MyCustomCommand
    {
        public static readonly RoutedUICommand AddCommand = new
            RoutedUICommand("Add Command", "AddCommand", typeof(MyCustomCommand));
        public static readonly RoutedUICommand DeleteCommand = new
            RoutedUICommand("Delete Command", "DeleteCommand", typeof(MyCustomCommand));
        public static readonly RoutedUICommand CopyCommand = new
            RoutedUICommand("Copy Command", "CopyCommand", typeof(MyCustomCommand));
    } 
    #endregion


As you see in above code display, we have created a static class with 3 static readonly commands of type RoutedUICommand.

Now to use it in XAML behind we need to add the namespace.

image3.gif

Let's have three buttons such Add, Edit and Copy. Which would operate on a ListBox item.

So let's do the design.

image4.gif

The following is the reference for the XAML.

image5.gif

Code view of the above image


Now let's have the Commands Binding for the ListBox.

image6.gif

Code view of the above example


  
    
    
    
  

As you see in above XAML display we have the Commands attached and we handled two events such as CanExecute and Executed for all the Commands.

Now let's set the CommandTarget and CommandParameter wherever required.

image7.gif

Code view of the above image

Now let's have the events handled.

Let's have the Add Command first.

image8.gif

Code view of the above image

#region Add
private void CanExecuteAdd(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = !String.IsNullOrEmpty(txtNewItem.Text) && 
        !lbItems.Items.Contains(txtNewItem.Text);
    e.Handled = true;
}

private void AddExecuted(object sender, ExecutedRoutedEventArgs e)
{
    ListBox listbox = sender as ListBox;
    if (listbox != null)
    {
        listbox.Items.Add(e.Parameter);
        txtNewItem.Clear();
        e.Handled = true;
    }
} 
#endregion

Then similarly Delete Command.

image9.gif

Code view of the above picture

#region Delete
private void CanExecuteDelete(object sender, CanExecuteRoutedEventArgs e)
{
    ListBox listbox = sender as ListBox;
    if (listbox != null)
    {
        e.CanExecute = listbox.SelectedItem != null;
        e.Handled = true;
    }
}

private void DeleteExecuted(object sender, ExecutedRoutedEventArgs e)
{
    ListBox listbox = sender as ListBox;
    if (listbox != null)
    {
        if (listbox.SelectedItem != null)
        {
            listbox.Items.Remove(listbox.SelectedItem);
            e.Handled = true;
        }
    }
} 
#endregion

And finally Copy Command.

image10.gif

Code view of the above image

#region Copy
private void CanExecuteCopy(object sender, CanExecuteRoutedEventArgs e)
{
    ListBox listbox = sender as ListBox;
    if (listbox != null)
    {
        e.CanExecute = listbox.SelectedItem != null;
        e.Handled = true;
    }
}

private void CopyExecuted(object sender, ExecutedRoutedEventArgs e)
{
    ListBox listbox = sender as ListBox;
    if (listbox != null)
    {
        if (listbox.SelectedItem != null)
        {
            listbox.Items.Add(listbox.SelectedItem);
            e.Handled = true;
        }
    }
} 
#endregion

That's it. Let's run the application and see how we managed to use CustomRoutedUI Commands.

image11.gif

image12.gif

image13.gif

You can also download sample project used in above example.

Hope this article helps.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend