2014-09-18 36 views
1

比方說,我有這樣的用戶控件: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

http://stackoverflow.com/questions/4630218 /指定 - 這屬性-去-之間最開閉標籤的在-XAML – 2014-09-18 17:37:29

回答

0

正如意見建議李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 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> 
相關問題