C# 带有内容的Windows Phone 8用户控件

C# 带有内容的Windows Phone 8用户控件,c#,silverlight,windows-phone-8,C#,Silverlight,Windows Phone 8,我想创建一个类似仪表的用户控件,它将根据特定的DataContext对象值在背景中显示一组宽度成比例的矩形。为了简单起见,我在下面的示例中硬编码了矩形图 <UserControl x:Class="MyApp.UI.BlockGauge" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml

我想创建一个类似仪表的用户控件,它将根据特定的DataContext对象值在背景中显示一组宽度成比例的矩形。为了简单起见,我在下面的示例中硬编码了矩形图

<UserControl x:Class="MyApp.UI.BlockGauge"
    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:DataContext="{d:DesignData SampleData/SampleViewModelSampleData.xaml}"
    d:DesignWidth="468" 
    d:DesignHeight="120"
    Name="GaugeControl">    

    <Grid x:Name="LayoutRoot"  Margin="0">
        <StackPanel x:Name="RootPanel">
            <Canvas x:Name="BackgroundCanvas"  Height="{Binding ActualHeight, ElementName=RootPanel}" Width="{Binding Width, ElementName=LayoutRoot}">
                <ContentPresenter Canvas.ZIndex="0"/>
                <Rectangle Width="240" Height="{Binding ActualHeight, ElementName=BackgroundCanvas}" Fill="{StaticResource PhoneAccentBrush}" Canvas.ZIndex="-20" />
                <Rectangle Width="80" Height="{Binding ActualHeight, ElementName=BackgroundCanvas}" Fill="Green" Canvas.ZIndex="-20" Canvas.Left="240"/>
                <Rectangle Width="40" Height="{Binding ActualHeight, ElementName=BackgroundCanvas}" Fill="Orange" Canvas.ZIndex="-20" Canvas.Left="320"/>
            </Canvas>
        </StackPanel>
    </Grid>
</UserControl>

我决定使用userControl的主要原因是基于绑定到它的视图模型对象计算矩形

我希望此控件仅在backgound上工作,并允许父级设置内容。每当我设置一个内容-所有用户控件内容都被它覆盖,我可以看到一个新的内容,而不是硬编码的矩形。我想要在动态背景上定制内容

我曾尝试在控件内部使用模板,但它说用户控件不支持模板属性

<UserControl.Template>
    <ControlTemplate>

    </ControlTemplate>
</UserControl.Template>


如何在动态绘制的背景上呈现注入内容?

为什么不将背景分层到矩形下方?@wiredparie您能解释更多吗?如何通过代码操纵矩形?在用户控件中,只需编写代码即可操纵矩形?您可以在画布中的矩形下方按Z顺序排列图像。如果您想添加对模板等的完整/简单支持,您需要编写一个
控件
。。。。我有一个在用户控件中操作矩形的代码,但是模板设置不起作用。您需要创建自己的属性来存储模板,并尽可能使用内容控件来呈现它。