Extremely Confused About ContentPresenter

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!

+4
source share
1 answer

This is because the contents of the control are the entire StackPanel that you wrote manually; when you install new content, the StackPanel is replaced.

This script requires a ControlTemplate; after all, that would be very simple. The starting point may be the default style for managing content ; place the style inside the ResourceDictionary (for example, in the <ContentControl.Resources> section of your user control), and you are ready to go; all you have to do is add a grid and a button inside this template.

Note that the style I linked sets a default value for any ContentControl available; so that it applies only to your control, and not to any child elements that may appear inside it, add x:Key="someKey" to the style and explicitly set the StyleControl Style property to Style={StaticResource someKey} .

Let me know if you need more information; also, maybe I'm wrong, and there may be an easier way, but I doubt it; the Content property should behave exactly as you described.

+5
source

All Articles