Sunday, February 05, 2012    
Blog  

OpenLight Blog

Using the “Hisowa Simple PopUp Behavior” in a DataGrid

Aug 12

Written by:
8/12/2010 6:35 AM  RssIcon

image

In the http://www.codeproject.com/KB/silverlight/HisowaSimplePopUpBehavior.aspx article, it states that there was assistance by me. My assistance was mainly running each build (Haruhiro simply emailed the project to me each day), trying a few implementations, and complaining:

  • I need to pass a parameter, any parameter, of any type, in and get it in the response.
  • I need to bind to an ICommand, that will be called when the Popup is complete. In the ICommand, I need the result of the Popup, AND my custom object.
  • (and on and on…)

The primary scenario that I had in mind, is that I wanted to EASILY implement a Popup confirmation box for my DataGrid example.

Let’s give it a spin and see how it works out: 

The PopUp Behavior

image

I practically begged Haruhiro to not create an assembly (a .dll), but to simply make his Popup Behavior a single .cs file. I felt that developers would find it easer to make any needed changes.

image

When you open up the download (SimplePopUpBehaviorSample.zip), you will see a few files, but you only need to copy the HisowaSimplePopUpBehavior.cs file to your own project. 

In Your Own Project…

image

I simply add the file to my DataGrid project.

image

I then add a reference…

image

… to System.Windows.Controls. The Behavior creates a ChildWindow in code, and the ChildWindow class is contained in this assembly.

I then build the application so that the behavior will be available for use in the application. 

Alter The Destination ICommand

I alter my existing ICommand from this:

    #region DeleteCommand
    public ICommand DeleteCommand { get; set; }
    public void Delete(object param)
    {
        // Clear the Comment
        objRIACommentsCollectionWrapper = (RIACommentsCollectionWrapper)param;
        DeleteRIAComment(objRIACommentsCollectionWrapper.objRIAComment.CommentID);
    }
 
    private bool CanDelete(object param)
    {
        // Do not allow if there is no Current RIAComment
        return (param as RIACommentsCollectionWrapper != null);
    }
    #endregion

To this: 

    #region DeleteCommand
    public ICommand DeleteCommand { get; set; }
    public void Delete(object param)
    {
        // Clear the Comment
        HisowaSimplePopUpBehaviorResult popupResult = (HisowaSimplePopUpBehaviorResult)param;
        if (popupResult.DialogResult == true)
        {
            objRIACommentsCollectionWrapper = (RIACommentsCollectionWrapper)popupResult.InputParameter;
            DeleteRIAComment(objRIACommentsCollectionWrapper.objRIAComment.CommentID);
        }
    }
 
    private bool CanDelete(object param)
    {
        // Do not allow if there is no Current RIAComment
        return (param as HisowaSimplePopUpBehaviorResult != null);
    }
    #endregion

Basically, I am receiving the HisowaSimplePopUpBehaviorResult object instead of the RIACommentsCollectionWrapper object as a parameter.

The HisowaSimplePopUpBehaviorResult contains the DialogResult and the previous RIACommentsCollectionWrapper object.

This allows me to see what answer the user gave to the Popup, and to have access to the DataContext object that I need so that I know what to delete.

Using The Behavior

image

In Expression Blend, I simply delete the existing Behavior that simply deleted the item on the DataGrid.

image

I drop the HisowaSimplePopUpBehavior on the Button instead.

image

I bind the ReturnICommand to the Delete ICommand that I altered earlier.

image

I bind the CustomParameter to the DataContext of the contentPresenter.

The Behavior in Action

image

When I run the project the Behavior just works!

The only problem is, it doesn’t match the rest of my application which has the Bubble Cream Theme applied to it.

So I open up the code for the Behavior (this is easy to do because it’s just a single .cs file), and change:

    //Make it a PopUp
    _childWindow.Content = _grid;
    PopUp = _childWindow;

To:

    //Make it a PopUp
    BubbleCremeTheme BCT = new BubbleCremeTheme();
    BCT.Content = _grid;
    _childWindow.Content = BCT;
    PopUp = _childWindow;

image

Now, it matches a little better.

(Yes, I already sent Haruhiro a email ‘complaining’ about the desire to have the PopUp do this automatically, and to also apply the style to the PopUp border).

I Didn’t Think It Was Possible

When he originally told me he wanted to make a Popup Behavior, I did not think it was possible. however, who am I to tell another Developer what he cannot, or should not do?

Criticizing and attacking other developers is not helpful. So I just smiled and said, “sounds like a great idea, let me know if I can help”. I figured at the least he would learn a lot about making Behaviors.

Even though I was sure I was right at the time, I was clearly wrong. It’s a good thing for me, I simply decided to be supportive. 

The code is at this link:

http://silverlight.adefwebserver.com/files/RIADataGridDeleteConfirmation.zip

3 comment(s) so far...


Gravatar

Re: Using the “Hisowa Simple PopUp Behavior” in a DataGrid

"Matches a little better"??? - Oh that did amuse me. It's lipstick on a pig!!! LOL

But in all seriousness: Great example Michael, & idiot (Designer) friendly!!!

By Evil Beezle on   8/12/2010 8:26 AM
Gravatar

Re: Using the “Hisowa Simple PopUp Behavior” in a DataGrid

@Evil Beezle - Yeah I have overused the Bubble Cream Theme :)

This Behavior also works in pure no code scenarios, because it has a Dialog result property you can bind to anything including a DataStore property.

Maybe some "Designer" (hint hint) will make a Blog post about that... :)

By admin account on   8/12/2010 8:57 AM
Gravatar

Re: Using the “Hisowa Simple PopUp Behavior” in a DataGrid

I just finished working my way through the original project. This is a great example of a practical application that doesn't obscure the details.

By Richard Waddell on   8/22/2010 5:32 PM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 
  
Copyright 2009 by OpenLightGroup.net   |  Privacy Statement  |  Terms Of Use