×

First time here?

You are looking at the most recent posts. You may also want to check out older archives. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS or email. Thank you for visiting!

The source code for the Silverlight 2 controls was just published!  This is the source for the controls as shipped in the runtime and the SDK.  When you download the project you’ll see the structure:

Image of source project tree in Visual Studio

The source, released under the Microsoft Public License (Ms-PL), includes the source for the following controls in the runtime:

    • ButtonBase
    • Button
    • HyperlinkButton
    • CheckBox
    • RadioButton
    • ToggleButton
    • RangeBase
    • ProgressBar
    • Slider
    • ScrollBar
    • Thumb

and the following from the SDK (part of the Silverlight Tools for Visual Studio):

    • Calendar
    • DatePicker
    • DataGrid
    • TabControl
    • GridSplitter

Unit tests for the SDK controls have also been included as well!

With the release of this source code as well as having access to the source for the Silverlight Toolkit, you should have some great base implementations to extend and learn from!  Get the code here: Source code for Silverlight 2 controls.


This work is licensed under a Creative Commons Attribution By license.


1/8/2009 2:30 AM | # re: Source code for Silverlight controls published
That's exciting news! What a nice way for learning :-)
Thanks Tim
1/8/2009 2:43 AM | # re: Source code for Silverlight controls published
Tim,

This is really useful, digging around in the controls and other peoples code in general is a great way to learn different techniques.

Great to see them shipping with the tests included too,

Bob

http://colaab.com
1/8/2009 2:44 AM | # re: Source code for Silverlight controls published
Tim,

This is really useful, digging around in the controls and other peoples code in general is a great way to learn different techniques.

Great to see them shipping with the tests included too,

Bob

http://colaab.com
1/8/2009 2:53 AM | # re: Source code for Silverlight controls published
Tim,

As usual..very helpful and informative ! :)

btw,bit out of topic..any plan to cover up "PageTurn" n "Video Carousal" demos in coming days by some demo or screencast?, the one available are with old version,tried to convert them..looks painful.

Vikram.
1/8/2009 3:17 AM | # re: Source code for Silverlight controls published
Great! I've been waiting for this!

Looking at this code is a great way of learning how to make controls. And not only how controls can be made, but also how they should be made (assuming of course that you at MS use best practises, which I'm guessing you are).
1/8/2009 7:50 AM | # re: Source code for Silverlight controls published
This is fantastic news! Big Kudos to Microsoft.
1/8/2009 9:06 AM | # re: Source code for Silverlight controls published
Wow this is awesome!
Gravatar
1/8/2009 11:50 AM | # re: Source code for Silverlight controls published
Is there any chance this will ever be included in Visual Studio so that you can step into the code while debugging? I wish Visual Studio came with this (as well as all of the .NET Framework code) by default.
1/8/2009 12:32 PM | # re: Source code for Silverlight controls published
It's really great to see the source code for some of the controls.

Just curious why the source to the rest of the controls wasn't released?
1/9/2009 4:21 AM | # re: Source code for Silverlight controls published
Hi Tim

It would be great to get the source code to the controls built into the Silverlight runtime (System.Windows) - especially for the ComboBox control so we can create our own version that fixes the bugs and omissions (particularly the lack of SelectedValuePath and SelectedValue properties, and recalculating the dropdown height for cascading dropdown list scenarios) as described in the "The ComboBox Nightmare" section in my article here:

www.silverlightshow.net/.../...ication-Part-5.aspx

Chris Anderson
1/9/2009 4:24 AM | # re: Source code for Silverlight controls published
Hi Tim

It would be great to get the source code to the controls built into the Silverlight runtime (System.Windows) - especially for the ComboBox control so we can create our own version that fixes the bugs and omissions (particularly the lack of SelectedValuePath and SelectedValue properties, and recalculating the dropdown height for cascading dropdown list scenarios) as described in the "The ComboBox Nightmare" section in part 5 of my article series on Building A Line Of Business Application on the SilverlightShow.net website (I can't provide a link as your comment system rejects my comment with one in it).

Chris Anderson
1/9/2009 4:29 AM | # re: Source code for Silverlight controls published
Hi Tim

It would be great to get the source code to the controls built into the Silverlight runtime (System.Windows) - especially for the ComboBox control so we can create our own version that fixes the bugs and omissions (particularly the lack of SelectedValuePath and SelectedValue properties which limits its use in line of business applications, and recalculating the dropdown height for cascading dropdown list scenarios) as described in the "The ComboBox Nightmare" section in part 5 of my article series on Building A Line Of Business Application In Silverlight on the SilverlightShow website here:

www.silverlightshow.net/.../...ication-Part-5.aspx

Do you know if that's likely at all?

Chris Anderson
1/9/2009 7:24 AM | # re: Source code for Silverlight controls published
Sorry for the repeated comments Tim - I kept getting a 500 error when publishing and didn't realise they were being posted. The last one was the one I wanted posted.

Chris Anderson
1/20/2009 2:49 AM | # re: Source code for Silverlight controls published
I recently extended the combobox to fix the issues about SelectedValue and SelectedValuePath. Hope this is a help!

here is the code:

public class ComboBoxExtended : ComboBox
{
public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(ComboBoxExtended), new PropertyMetadata(new PropertyChangedCallback(SelectedValuePathPropertyChanged)));
public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(object), typeof(ComboBoxExtended), new PropertyMetadata(new PropertyChangedCallback(SelectedValuePropertyChanged)));

public ComboBoxExtended()
: base()
{
base.SelectionChanged += new SelectionChangedEventHandler( ComboBoxExtended_SelectionChanged );
}

protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
object val = GetValue(SelectedValueProperty);
ResetSelectedItem(val);
}

void ComboBoxExtended_SelectionChanged( object sender, SelectionChangedEventArgs e )
{
if ( SelectedItem != null && !string.IsNullOrEmpty( SelectedValuePath ) )
{
try
{
SetValue(ComboBoxExtended.SelectedValueProperty, SelectedItem.GetType().GetProperty(SelectedValuePath).GetValue(SelectedItem, null));
}
catch
{
//Add exception handling
throw new NotImplementedException();
}
}
}

static void SelectedValuePathPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
{
if (e.Property == SelectedValuePathProperty)
{
ComboBoxExtended me = d as ComboBoxExtended;
if (me != null)
{
//do something...
}
}
}


static void SelectedValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.Property == SelectedValueProperty)
{
ComboBoxExtended me = d as ComboBoxExtended;
if (me != null)
{
try
{
me.ResetSelectedItem(e.NewValue);
}
catch (Exception ex)
{
//Add exception handling
throw new NotImplementedException();
}
}

}
}

private void ResetSelectedItem(object pNewValue)
{
if (Items.Count > 0)
{
object[] parameters = new object[0];
foreach (object item in Items)
{
object ival = item.GetType().GetProperty(SelectedValuePath).GetValue(item, parameters);
if ((ival != null) && (ival.Equals(pNewValue)))
{
SelectedItem = item;
break;
}
}
}
}

public string SelectedValuePath
{
get
{
return (string)GetValue(ComboBoxExtended.SelectedValuePathProperty);
}
set
{
SetValue(ComboBoxExtended.SelectedValuePathProperty, value);
}
}
public object SelectedValue
{
get
{
return GetValue(SelectedValueProperty);
}
set
{
SetValue(ComboBoxExtended.SelectedValueProperty, value);
}
}

}
1/22/2009 6:02 PM | # re: Source code for Silverlight controls published
I just came across the ComboBox nightmare. How do I make the size of the drop-down change after it was opened the first time?
Pah-lease help!
1/23/2009 3:13 AM | # re: Source code for Silverlight controls published
Hi Nick - see my article linked in a comment above where I demonstrate how to remove the combobox from the page and add it again. From all my attempts I couldn't find any other way to fix the problem other than that brute force method. It's not pretty but it works around the bug.

Thanks for posting that code Stefan - I did try a similar thing when I was looking at the problem but couldn't get it to work the way I wanted or wasn't happy with that solution (I can't remember why now) and decided to go with the value converter method I detailed in my article instead. But your solution looks better so I'll give it a go.

Chris
1/23/2009 6:56 AM | # re: Source code for Silverlight controls published
Hey Chris,
I saw your article series (which is great by the way), but from my point of view such solution would increase the complexity and reduce the stability of the code.
I came up with my own solution eventually (if I have time, I'll publish it in more detail). Basically the idea is to use a StackPanel as ItemsPanel for the ComboBox control. Add a Loaded event handler to the StackPanel (to be able to catch it every time before the expansion). Go up the visual tree using VisualTreeHelper.GetParent method starting with the StackPanel and selectively changing the sizes of some of the object within that tree.
It only took me 8-10 hours to figure it out:)
Here is the relevant Xaml code (pay attention to the Loaded event handler):
<ComboBox
x:Name="MyCombo"
x:FieldModifier="public">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel
Loaded="MyComboStack_Loaded"/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>

And here is the implementation of the MyComboStack_Loaded function:

private void MyComboStack_Loaded(object sender, RoutedEventArgs e)
{
StackPanel myPanel = (StackPanel)sender;
double height = 0;

int numChildren = VisualTreeHelper.GetChildrenCount(myPanel);

height = numChildren * 22;

DependencyObject obj = VisualTreeHelper.GetParent(myPanel);

obj = VisualTreeHelper.GetParent(obj);
obj = VisualTreeHelper.GetParent(obj);
obj = VisualTreeHelper.GetParent(obj);

obj = VisualTreeHelper.GetParent(obj);

ScrollViewer scrollViewer = (ScrollViewer)obj;
obj = VisualTreeHelper.GetParent(obj);
((FrameworkElement)obj).Height = height;
}
One combo item height 22 I figured out by experimentation, I still do not know how to make the code work for arbitrary item heights.

2/5/2009 6:03 AM | # re: Source code for Silverlight controls published
Thx Chris

I have that ComboboxExtended in use several times. it really seems to work :)

Greets
7/1/2011 3:11 AM | # re: Source code for Silverlight controls published
This is a good site to spent time on .I just stumbled upon your informative blog and wanted to say that I have really enjoyed reading your blog posts. I will be your frequent visitor, that’s for sure....

free cell phone spy software

 
Please add 8 and 7 and type the answer here:

DISCLAIMER:

The opinions/content expressed on this blog are provided "ASIS" with no warranties and are my own personal opinions/content (unless otherwise noted) and do not represent my employer's view in any way.