If you are looking to learn about Silverlight development, here is your chance.  Our local Microsoft Developer Evangelists are running a free full day event this Saturday featuring a good variety of information. 

To learn more or register, click here.

If you are a beginner or haven’t gotten started yet, this is a great opportunity.  I’m not a beginner and my weekends are pretty busy these days.  So initially, I was going to skip this event.  But from my conversation with Dani Diaz last night, it sounds like there will be a decent range of information here, so I think I will attend too. 

I don’t know the full agenda but some of the speakers include Dani, Lindsay Rutter, Bill Wolff and Pete Brown.  That’s a good line up but to be perfectly honest, Pete Brown is the big draw for me.  I’ve been reading his blog since I started doing Silverlight development, this guy really knows his stuff.  One thing I’ve learned over time is that if a good speaker if giving a talk, even on a topic that I think I know fairly well, it is worth attending.  You never know what kind of tidbit you may pick up from them!

Hope to see you there.

One thing I really hate about doing Silverlight development is trying to search for good information on the Internet.  When I look for a solution to a problem I am having, I always run into a lot of “noise” in the form of outdated posts that aren’t relevant anymore.  There are a ton of questions, answers, and sample solutions on the web from developers working with the Silverlight 2 Beta release.  I know this was all helpful information to share at the time, but a lot of it is useless now.  The problem is, sometimes you have to read the post a bit in order to figure out that the developer was on the Beta.  I’ve gotten to the point where the first thing I do is look at the date on the post before I read on.  What makes this even more complicated, is sometimes the information in these posts is still relevant and very helpful! 

Oh well, at least we are lucky to be developers in an age where so much information is shared among members of the community.  Can you imaging having to read manuals for all your information ;) ?  I don’t mean to discourage anyone from posting.  Don’t take me too seriously, I’m just ranting.

The other day I was working on a Silverlight app when I got this error:  Layout Cycle Detected.  The good news is that Silverlight actually gave me an error message that meant something.  That is unusual for Silverlight.  Of course the message didn’t tell me what caused it, or how to fix it!  Luckily, there is this thing called the Internet ;-).  I found some information on David Yack’s blog (I don’t know him) that helped.

It turns out that Silverlight doesn’t like it if you put too many TextBoxes in a ListBox (or possibly any other “repeater” kind of list control).  I’m not sure why it only applies to TextBoxes and not other controls, but here is my understanding of the problem…  Each time you add a TextBox to a ListBox it needs to figure out how much space to give the control, as well as all the other controls around it.  It figures that out, and then sizes all of the controls that don’t have specific sizes set, and then lays everything out for you.  But I guess in a list, it needs to do this over and over and over again and Silverlight thinks it has gone into an endless loop, so at a certain point it cuts it off and says “Layout Cycle Detected”.  Each time a control gets resized, the controls around it get resized, and that goes on and on until you get to the top level control.  I guess I can understand that it could be a problem, but I think the developers at Microsoft have got to come up with a better way to resolve this.  If I want to put a lot of TextBoxes in a list, I should be allowed to do so!  Also, the limit is supposedly 250 TextBoxes, but I am pretty sure I had less than that.

There are a few ways to get around the problem, as far as I know.  In my case there are a bunch of controls that made up the DataTemplate for my ListBox.  Since they are in a StackPanel, I put a fixed size on the StackPanel.  That prevents the “bubble up” of resizing going all the way to the page level.  That seemed to fix my problem most of the time, but not all of the time.  I could probably have kept playing with the layout and putting fixed sizes on more panels.  Instead, I gave in. 

I didn’t want to fight with Silverlight any more so I changed all of my TextBoxes to TextBlocks.  Then I set the the ListBox items up so that you can switch them into “Edit Mode”.  The problem here is that the ListBox doesn’t have built in support for that.  So here is how I did it.  By the way, this is not dependent upon the “SelectedItem” in the ListBox, so you can have multiple items in EditMode at the same time.

The purpose of my list is to display a list of Services to the user.  My ListBox is bound to a collection: ObservableCollection<ServiceListItem>.  ServiceListItem is just a class that represents my data.  Among it’s other properties, the ServiceListItem has a property for IsInEditMode.  It is nothing fancy:

            public bool IsInEditMode
            {
                get { return _isInEditMode; }
                set
                {
                    if (_isInEditMode == value)
                        return;
                    _isInEditMode = value;
                    NotifyPropertyChanged("IsInEditMode");
                }
            }
            private bool _isInEditMode;

In my DataTemplate for the ListBox I have two StackPanels, one for ReadOnly mode and one for Edit Mode.  The ReadOnly template has a button to switch to EditMode.  The EditMode template has two buttons.  The Cancel button reverts the data to it’s original state and switched to ReadOnly mode.  The Save button saves the data and switched to ReadOnly Mode.  Here is the one that puts the template into Edit Mode (nothing fancy here):

<Button x:Name="EditService" Click="EditService_Click">
    <Button.Template>
        <ControlTemplate>
            <Image Source="../images/pencil.png" 
                   Height="10" 
                   Width="10" 
                   Cursor="Hand" 
                   VerticalAlignment="Center" 
                   HorizontalAlignment="Center"/>
        </ControlTemplate>
    </Button.Template>
</Button>

The code for the button click is pretty simple too:

        private void EditService_Click(object sender, RoutedEventArgs e)
        {
            ServiceListItem item = (ServiceListItem)((Button)sender).DataContext;
            item.IsInEditMode = true;
        }

Actually, the only cool part of this is how the control figures out which StackPanel to display.  Each one has it’s Visibility Property bound to the IsInEditMode property!

                           <StackPanel x:Name=”EditPanel”
                                       Visibility=”{Binding IsInEditMode, Converter={StaticResource BoolVisibilityConverter}}” 
                                       Orientation=”Horizontal” Height=”20″ >

The trick is the Converter.  We’re using a Converter that converts a boolean value into a value from the Visibility Enumeration:

 

    public class BoolVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || string.IsNullOrEmpty(value.ToString()) || !(bool)value)
                return Visibility.Collapsed;

            return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // we don't need it to convert back but we could implement that later.
            return value;
        }
    }
Don't forget to add the Converter to your user control's resources:
    <UserControl.Resources>
        <ClientUtilties:BoolVisibilityConverter x:Key="BoolVisibilityConverter" />
        <ClientUtilties:BoolInverseVisibilityConverter x:Key="BoolInverseVisibilityConverter" />
    </UserControl.Resources>

And it all just works!
From this, you should be able to figure out the other end of this, switching back to Read Only Mode. 
I hope this helps.  

If you read my last post about Ben, you’ll know that the winter wasn’t going so well.  It seemed like Ben was always sick and in a “bad mood”.  Well not anymore!  The little guy that we love is back and crazier than ever.  One day he just changed completely back to his old self.  These days he is sleeping great, eating great, playful as can be and all about having fun!

Ben is loving “real” food.  He really wants to feed himself so we let him do that most of the time.  He eats pasta, scrambled eggs, sweet potatoes and regular potatoes, chicken nuggets, fish sticks, pears, turkey meatloaf, veggie burgers, pizza (crust), peas, broccoli, carrots, mac and cheese and a bunch of other stuff.  He watches what Mommy and Daddy eat and always wants food off of our plates.  Plus, he is taking his bottles more too.  So all that is going well.

Play time is completely different these days.  Benny really seems to “get” the game.  I don’t know what goes on in his mind but he is really starting to learn things.  The little gears are really turning in his head.  He loves to play with his Fisher Price “House”.  He plays peek-a-boo by lifting the window up and down.  He also loves his “Little People“.  And he has car that he can ride on and walk behind.  We are noticing more hand coordination too as he can put balls into a slot or put his blocks together (sometimes).  He’s very proud of himself too, especially when he is walking (with our help), standing – yes on his own for a few seconds at a time, and other fun stuff.  Yesterday I said “How big is Benny” and up went his hands.  I had been trying that for a while with him with no result.  But it just clicked and how he does it all the time. 

Ben is really looking forward to turning 1 year old.  He had a lot of fun with his cousin Grace at her Birthday party.  She’s older by 10 days!  Check out the picture of them together, it is pretty funny because Grace is big for her age, while Ben is pretty small.

OK, here are some of my favorite pictures from the last few weeks.  Oh, by the way, Benny got his first haircut too.

DSC03884 DSC03915 DSC03922 DSC03967 DSC03976 DSC03982 DSC03983