For the bartender, I’m going pure WPF. Since Expression Blend 3.0 now can import Adobe Illustrator files, my friend Ian can use the tools he knows best and produce me some amazing graphics. I don’t do amazing graphics so I’m going to show how to do it with simple shapes.
Based off the code in my prior post for my Big Button, I’ll be dynamically adding stuff in. Before and After Screen shots:

Base XAML as it was slightly modified. I’ve added in another grid to load the items into.
<Button Name="bigButton" Margin="14,6,14,71.517" Click="bigButton_Click">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Name="DyanmicXamlLoad">
<Image Name="image1" Stretch="UniformToFill"
Source="Koala.jpg" StretchDirection="Both" Margin="10"
VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
<Label Grid.Row="1" HorizontalAlignment="Center" FontSize="20" Padding="0">
change background
</Label>
</Grid>
</Button>
Now the c# for the loading XAML:
var stringReader = new StringReader(
@"<Grid xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<Ellipse Height=""100"" Width=""100"" StrokeThickness=""5""
Stroke=""Black"" Name=""testObject"" Fill=""Gold""/>
</Grid>"
);
var xmlReader = new XmlTextReader(stringReader);
var element = (UIElement)XamlReader.Load(xmlReader);
DyanmicXamlLoad.Children.Clear();
DyanmicXamlLoad.Children.Add(element);
So key things when adding in dynamic code, you need to remember to add in the name space else the XamlReader.Load will fail. Another thing is when you load this dynamically loaded element, it loads in under a different name scope. Due to this, the FindName won’t return the dynamically loaded elements if you read the documentation. MSDN has an article on this and gives some work arounds for this.
My work around is the following.
Brush brush = new SolidColorBrush(getRandomColor());
if (DyanmicXamlLoad.Children.Count > 0)
{
var element = ((FrameworkElement)DyanmicXamlLoad.Children[0]).FindName("testObject");
if (element != null && element is Shape)
((Shape)element).Fill = brush;
}
So my work around is knowing the element I’m loading into, verifying it has children. If it does, grab the first one and then do the FindName on it.
But now I can dynamically load my drink glasses and change the color of the liquid!