Jesse Liberty - Silverlight Geek

By, For and About Silverlight Developers

Blend, VS, Events and C#

From now until the summer I'll be working on videos, tutorials, presentations and blogging, but I'll also be writing Programming Silverlight 2 with Tim Heuer. Rather than convincing you that I'm continually hawking my book, I'd rather use the process of writing it as an opportunity to provide good, free, and I hope interesting material for this blog. (Write to me if I get the balance wrong!).

As an example, I ran into a bit of C# in what I'm writing in the first chapter that I thought made for an interesting example of how a Silverlight 2 programmer can? must? juggle the three principle technologies of (a) Visual Studio and (b) Blend and (c) a programming language of choice (which I can't quite understand why that wouldn't be C# or VB but I do know lots of folks disagree, and that in itself will make an interesting blog entry!)

So this blog entry may be of interest only for those of you who enjoy a bit of C#.

The context

This is early in the book and I'm laying out 9 checkboxes, used to designate search criteria. The first checkbox is "Any". If that is clicked the others are invisible,

AnyCheckBox

If Any is unchecked, all the other criteria appear,

AnyUnchecked

 

The book then goes on to show a number of things, including how to create the refining characteristics panels and how to overlay them, etc. etc. but the part I want to focus on here is a tiny detail: how you set the visibility flag on the check boxes. 

Here is the code I used.

private void AnyPropertyCheckBox_Click(object sender, RoutedEventArgs e)
{
    bool? isVisible = !AnyPropertyCheckBox.IsChecked;
    foreach (UIElement uie in LayoutRoot.Children)
    {
        CheckBox cb = uie as CheckBox;
        if (cb != null)
        {
            if (cb.Tag != null
                && cb.Tag.ToString().ToUpper() == "SEARCHCRITERIA")
            {
                cb.Visibility = isVisible == true ? 
                   Visibility.Visible : Visibility.Collapsed;
            }   // end if tag match
        }       // end if check box
    }           // end for each element
}               // end method

 

I'll walk through the entire method in just a second, but the key c# construct here is the use of the ternary operator (?:)

cb.Visibility = isVisible == true ? 
    Visibility.Visible : Visibility.Collapsed;

This operator (?:) is called ternary as it is the only operator that takes three parts. Here is how you read it (inside out).  First you evaluate the truth part  (isVisible==true).  This could have been written as

!AnyPropertyCheckBox.IsChecked == true

or

AnyPropertyCheckBox != IsChecked

To simplify I used an interim nullable boolean variable.

In any case, if the truth part evaluates true, then whatever is on the left side of the colon (in this case Visibility.Visible) is assigned as the result of the expression, otherwise, whatever is on the right side is assigned.

I admit, it is a construct only a former C programmer can love.

Note that the code I used  is exactly the same as writing

private void AnyPropertyCheckBox_Click(object sender, RoutedEventArgs e)
{
    bool? isVisible = !AnyPropertyCheckBox.IsChecked;
    foreach (UIElement uie in LayoutRoot.Children)
    {
        CheckBox cb = uie as CheckBox;
        if (cb != null)
        {
            if (cb.Tag != null
                && cb.Tag.ToString().ToUpper() == "SEARCHCRITERIA")
            {
               if ( isVisible == true )
               {
                   cb.Visibility = Visibility.Visible;
                }
                else
                {
                   cb.Visibility =  Visibility.Collapsed;
                }
            }   // end if tag match
        }       // end if check box
    }           // end for each element
}               // end method

 

So, as promised, here is the line by line walk through

// Declare isVisible to be a nullable boolean variable
// (it can have three values: true, false or null
// If AnyPropertyCheckBox's property IsChecked is true, assign false
// otherwise assign true.  This is dangerous code as the property could
// be null, and in retrospect I'd rewrite this as
// bool? isVisible = ! (AnyPropertyCheckBox.IsChecked == true);
// which would assign false if the property is true or null
bool? isVisible = !AnyPropertyCheckBox.IsChecked;

Next we iterate through all the children of LayoutRoot which is a grid that contains all the UIElements on the page

foreach (UIElement uie in LayoutRoot.Children)
{
     // code to explain here
}

Within that loop we'll grab each element and cast it to type Checkbox. The cast will either work or return null. If it does not return null, we'll ask the temporary check box if its tag is null. If not, then we have a check box with a tag and we can see if that tag, when turned to a string matches the tag we're looking for

// try casting to a checkBox
CheckBox cb = uie as CheckBox;

// if the cast is not null, it really was a check box
if (cb != null)
{
    // see if the tag is null and if not if the tag as a string
    // matches what we're looking for
    if (cb.Tag != null
        && cb.Tag.ToString().ToUpper() == "SEARCHCRITERIA")
    {
        // code here
    }   
}       

Note that the test for the tag not being null and then turning the tag into a string is accomplished in a single if statement. That is safe if you get the order right and use the && symbol between the two expressions, because they will short circuit. That is, because both must be true, if the first is false, the second will never evaluate (which is a good thing, because if the first is false, then the Tag is null and trying to call ToString on it would throw an exception).

Finally, inside the if statement is the ternary operator where we set the visibility.

Not bad, but not obvious if you're not comfortable with a bit of C#.

Comments

BenHayat said:

Jesse, a few years ago, when I had started with Delphi, I was reading many books, and they all followed the same  pattern. I think one author would just re-phrase another author's statement and claim to write a new book. Until I came across a book called " Delphi How-To". Completely different than others, the author which was also a programmer, wrote the book based on the challenges that he came across during his development. So, every time he  resolved a new challenge, he added it into his book. It was the most successful and "Useful" book you could get your hand on and saved so much time. I've also ran into a similar book in C# called "C# Cookbook". Silverlight needs a book like this!

I'm almost getting a felling that your book might be going towards that direction.

Just to share my Sunday thoughts!

..Ben

# May 18, 2008 11:39 AM

Blend, VS, Events and C# | My Geek Solutions said:

Pingback from  Blend, VS, Events and C# | My Geek Solutions

# May 18, 2008 1:07 PM

writing characteristics said:

Pingback from  writing characteristics

# May 18, 2008 7:33 PM

Dew Drop - May 18, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - May 18, 2008 | Alvin Ashcraft's Morning Dew

# May 18, 2008 9:15 PM

raghuv said:

Hey Jesse,

I have a question regarding the dropdown control in Silverlight 2.0. Is there a plan to release controls like DropDown Control and the TreeView control in the upcoming releases of SL??

Thanks,

Raghu

# May 18, 2008 11:54 PM

jskeet said:

?: is more properly called the conditional operator. Yes, it happens to be the only ternary operator in C# at the moment, but that's not a good reason to call it the ternary operator. I know many people do so, but I don't think it's a good idea to encourage them :)

# May 19, 2008 11:31 AM

wazikhan said:

Hi jesse i realy like ur tutorail

i have a question from u.

i create a user control which have 5 buttons which are

(Home aboutus Contactus Project )

in Silverlight2.0.

i use this user control as a menue in Asp.Net page.

Now my Question is that how can i get event of button press event in "Asp.Net cs page". so that i can navigate through the site. when the user press home button i redirect my page to Home or press Contact so that i can redirect to Contatus page

# May 19, 2008 11:58 AM

jesseliberty said:

>>Is there a plan to release controls like DropDown Control and the TreeView control in the upcoming releases of SL??<<

All we've said publicly is that we will be releasing more controls in Beta 2. Since Beta 2 will be relased in Q2 (which means Real Soon Now) I think I'd best hold my peace.

>>?: is more properly called the conditional operator. Yes, it happens to be the only ternary operator in C# at the moment, but that's not a good reason to call it the ternary operator. I know many people do so, but I don't think it's a good idea to encourage them :)<<

Well, In theory, theory and practice are the same, but in practice, they never are. I've been programming in the C family since 1984 and the conditional operator has been the only ternary operator for those two decades. I feel like I'm on pretty safe ground. That said, you are 100% correct, it should be called the Conditional Operator.

>>how can i get event of button press event <<

You are asking how to pass an event from ASP.NET to a Silverlight Control.  There are videos on that in the Silverlight 1 area, but you are right, it is time for me to dedicate a set of videos to those interactions.

# May 26, 2008 12:09 PM