سؤال

I have created user control. I want to display XAML inside my usercontrol. Like that:

<UserControls:UserControl1 Header="Heading">
        <TextBlock Text="My Content" />
</UserControls:UserControl1>

Thats the usercontrol:

<UserControl x:Class="UserControls.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" MinHeight="200"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Style TargetType="ToggleButton">
            <!-- ... -->
        </Style>        
    </UserControl.Resources>
    <StackPanel>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding Path=Header}" Grid.Column="0" />
            <ToggleButton Name="ToggleButton" IsChecked="True" Grid.Column="2" />
        </Grid>
        <Rectangle Stroke="#c3c3c3" StrokeThickness="1" Height="1" StrokeDashArray="4 4" SnapsToDevicePixels="True" Focusable="False" />
        <!-- Content -->
    </StackPanel>
</UserControl>

Now how can I set the xaml code (e.g. <TextBlock Text="My Content" />), in my control?

هل كانت مفيدة؟

المحلول

You just need to add a ContentPresenter or ItemsPresenter depending on the item the presenter is added to.

In your case, if you wanted the content in the stack panel below the other items, you could place a Content Control and add a ContentPresenter inside like so.

<StackPanel...>
    <Grid ...>
        ...
    </Grid>
    <Rectangle .../>
    <!---Content here-->
    <ContentControl>
        <ContentPresenter/>
    </ContentControl>
</StackPanel>

If you only wanted to support more than one content item, then use some control that suports more than one content, and use <ItemsPresenter/> instead.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top