继承ContentControl可以很方便实现包含内容(Content)的控件,方案一:
自定义控件代码如下:

    public class DockPanel : ContentControl
    {
        
public DockPanel()
        {
            
this.DefaultStyleKey = typeof(DockPanel);
        }
    }

themes/Generic.xaml代码如下:

    <Style TargetType="local:DockPanel">
        
<Setter Property="Template">
            
<Setter.Value>
                
<ControlTemplate TargetType="local:DockPanel">
                    
<Grid>
                        
<Border>
                            
<Grid>
                                
<ContentPresenter Content="{TemplateBinding Content}" />
                            
</Grid>
                        
</Border>
                    
</Grid>
                
</ControlTemplate>
            
</Setter.Value>
        
</Setter>
    
</Style>

引用页面代码:

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:cinlap
="clr-namespace:SilverlightClassLibrary1;assembly=SilverlightClassLibrary1"
    Width
="400" Height="300">
    
<Grid x:Name="LayoutRoot" Background="Yellow" Width="400" Height="300" >          
        
<cinlap:DockPanel>
            
<cinlap:DockPanel.Content>
                
<StackPanel Background="Red"/>
            
</cinlap:DockPanel.Content>
        
</cinlap:DockPanel>
    
</Grid>  
</UserControl>

方案二:

自定义控件:
    public class MyContentControl : Control
    {
        
public static readonly DependencyProperty ContentPanelProperty =
            DependencyProperty.Register(
"ContentPanel"typeof(Panel), typeof(MyContentControl), null);

        
public MyContentControl()
        {
            
this.DefaultStyleKey = typeof(MyContentControl);
        }

        
public Panel ContentPanel
        {
            
get
            {
                
return (Panel)this.GetValue(ContentPanelProperty);
            }
            
set
            {
                
this.SetValue(ContentPanelProperty, value);
            }
        }
    }

themes/Generic.xaml代码如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
                    xmlns:local
="clr-namespace:SilverlightClassLibrary1" >
    
<Style TargetType="local:MyContentControl">
        
<Setter Property="Template">
            
<Setter.Value>
                
<ControlTemplate TargetType="local:MyContentControl">
                    
<Grid>
                        
<Border>
                            
<Grid>                                
                                
<ContentPresenter Content="{TemplateBinding ContentPanel}"/>
                            
</Grid>
                        
</Border>
                    
</Grid>
                
</ControlTemplate>
            
</Setter.Value>
        
</Setter>
    
</Style>
</ResourceDictionary>

引用页面代码:
    <Grid x:Name="LayoutRoot" Background="Yellow" Width="400" Height="300" >          
        
<cinlap:MyContentControl>
            
<cinlap:MyContentControl.ContentPanel>
                
<StackPanel Background="Red"/>
            
</cinlap:MyContentControl.ContentPanel>
        
</cinlap:MyContentControl>
    
</Grid>

补充:
Silverlight自带的内容控件中一般使在ContentPresenter时绑定了以下两个属性:

HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"

经试验,定义了这两个属性后,内容元素不能自动缩放,所以在自定义Content控件时,可视自已实际应用运用这两个属性。

posted on 2009-07-23 23:48  think8848  阅读(927)  评论(0编辑  收藏  举报