FYI I'm new to Silverlight.
Okay, so I want to create a simple user control containing a button plus additional XAML specified by the control client.
I searched on Google and found at least 30 different articles that were very confusing; especially because they talk about animating styles, setting up other controls that you donโt own, and other crap I'm not ready for.
This is what I did.
- In VS 2010, I right-clicked and added a new UserControl called MyControl
- In MyControl.xaml I changed LayoutRoot to StackPanel and added a button inside it
- In my MainPage.xaml, I added an instance of MyControl
- I added a TextBox as a child of this instance
- I tried to create and received a message stating that MyControl does not support Direct Content
Google is even more.
- I changed MyControl to inherit from ContentControl and updated xaml
- I added ContentPresenter in xaml to represent client "custom content"
Ok, it builds and the TextBox appears, but the button is missing.
Here is the relevant section from MainPage.xaml
<my:MyControl HorizontalAlignment="Left" Margin="49,26,0,0" x:Name="myContentControl1" VerticalAlignment="Top" Height="550" Width="389"> <TextBox Height="72" HorizontalAlignment="Left" Margin="166,339,0,0" Name="textBox1" Text="TextBox" VerticalAlignment="Top" Width="460" /> </my:MyControl>
Here is mycontrol.xaml
<ContentControl x:Class="ContentControlTest.MyControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" d:DesignHeight="480" d:DesignWidth="480"> <StackPanel x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" Orientation="Vertical"> <ContentPresenter/> <Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="78,254,0,0" Name="FooFoo" VerticalAlignment="Bottom" Width="160" /> </StackPanel> </ContentControl>
And here is MyControl.cs
using System.Windows.Controls; namespace ContentControlTest { public partial class MyControl : ContentControl { public MyControl() { InitializeComponent(); } } }
As I thought this worked, it is because the children of the control instance are set as the Content property in the ContentControl MyControl base class. ContentPresenter then โinsertsโ this content into the MyControl.xaml file, wherever it appears.
Although this is similar to how it works, in the process this is the โButtonโ that I defined in MyControl.xaml.
I try not to get into ControlTemplate, etc., which at the moment, if absolutely necessary.
Can someone with a hint please tell me what I'm doing wrong.
thanks!