UserControl可以用来格式化内容吗?

时间:2014-09-18 16:50:58

标签: wpf wpf-controls

假设我有这个UserControl:

<UserControl x:Class="BorderWithHeader"
             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" 
             d:DesignHeight="300" d:DesignWidth="300">

    <StackPanel Orientation="Vertical">
        <Border BorderThickness="1" BorderBrush="Gray" Height="50">
            <TextBlock Text="{Binding Header, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
        </Border>
        <Border BorderThickness="1" BorderBrush="Gray" Height="500">
            <!--I want to display complex content here (i.e. containers, grids, stackpanels, etc.)-->
        </Border>
    </StackPanel>
</UserControl>

后面的代码如下:

public partial class BorderWithHeader : UserControl
{
    public string Header
    {
        get { return (string)GetValue(HeaderProperty); }
        set { SetValue(HeaderProperty, value); }
    }

    public BorderWithHeader()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(string), typeof(BorderWithHeader), new FrameworkPropertyMetadata(OnHeaderPropertyChanged));


    private static void OnHeaderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
    }
}

我想在我的一个观点中使用它:

<viewsCommon:BorderWithHeader Header="Title">
    <!--I want to define complex content here (i.e. containers, grids, stackpanels, etc.)-->
</viewsCommon:BorderWithHeader>

标题绑定效果很好,但我无法理解如何使内容绑定工作。这甚至可能还是我以错误的方式接近这个?如果是这样,那么应该怎样做呢?

1 个答案:

答案 0 :(得分:0)

正如Lee O.在评论中所建议的那样,我将代码改为:

[ContentProperty("Body")]
public partial class BorderWithHeader : UserControl
{
    public object Body
    {
        get { return GetValue(BodyProperty); }
        set { SetValue(BodyProperty, value); }
    }

    public string Header
    {
        get { return (string)GetValue(HeaderProperty); }
        set { SetValue(HeaderProperty, value); }
    }

    public BorderWithHeader()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty BodyProperty = DependencyProperty.Register("Body", typeof(object), typeof(BorderWithHeader), new FrameworkPropertyMetadata(OnBodyPropertyChanged));
    public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(string), typeof(BorderWithHeader), new FrameworkPropertyMetadata(OnHeaderPropertyChanged));

    private static void OnBodyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
    }

    private static void OnHeaderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
    }
}

我的UserControl对此:

<UserControl x:Class="Garmin.Cartography.Omt.Tools.AdminBucket.UI.Views.Common.BorderWithHeader"
             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"
             xmlns:ap="clr-namespace:Garmin.Cartography.Omt.Tools.AdminBucket.UI.AttachedProperties"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <StackPanel Orientation="Vertical" Background="Red">
        <Border BorderThickness="1" BorderBrush="Gray" Height="50">
            <TextBlock Text="{Binding Header, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"></TextBlock>
        </Border>
        <Border BorderThickness="1" BorderBrush="Gray" Height="500">
            <ContentControl Content="{Binding Body, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"></ContentControl>
        </Border>
    </StackPanel>
</UserControl>