我想写一个ContentControl
来托管其他控件并给它们一个特殊的边框。
所以我添加了一个新的UserControl
,做了一个ContentControl
。它有一个网格,在外侧我想有边框。
所以第一个问题是:这是实现“有界”控制的好方法吗?
这是 XAML
<ContentControl x:Class="DemoApplication.Controls.ImpressedContentControl"
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"
Name="ImpressedContent"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Grid>
<Grid.Resources>
<ImageBrush x:Key="ImpressedLeftBrush"
ImageSource="/DemoApplication;component/Images/Impressed_left.png"
TileMode="FlipY"
Viewport="0,0,100,100"
ViewportUnits="Absolute" />
<ImageBrush x:Key="ImpressedRightBrush"
ImageSource="/DemoApplication;component/Images/Impressed_right.png"
TileMode="FlipY"
Viewport="0,0,100,100"
ViewportUnits="Absolute" />
<ImageBrush x:Key="ImpressedTopBrush"
ImageSource="/DemoApplication;component/Images/Impressed_top.png"
TileMode="FlipY"
Viewport="0,0,100,100"
ViewportUnits="Absolute" />
<ImageBrush x:Key="ImpressedBottomBrush"
ImageSource="/DemoApplication;component/Images/Impressed_bottom.png"
TileMode="FlipY"
Viewport="0,0,100,100"
ViewportUnits="Absolute" />
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="4" />
<RowDefinition Height="1*" />
<RowDefinition Height="4" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="4" />
</Grid.ColumnDefinitions>
<Grid Grid.Row="0"
Grid.Column="1"
Background="{DynamicResource ImpressedTopBrush}" />
<Grid Grid.Row="2"
Grid.Column="1"
Background="{DynamicResource ImpressedBottomBrush}" />
<Grid Grid.Row="1"
Grid.Column="0"
Background="{DynamicResource ImpressedLeftBrush}" />
<Grid Grid.Row="1"
Grid.Column="2"
Background="{DynamicResource ImpressedRightBrush}" />
<ContentControl Grid.Row="1"
Grid.Column="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Content="{Binding ElementName=ImpressedContent,
Path=ControlContent}" />
</Grid>
</ContentControl>
在代码隐藏中,我有一个DependencyProperty
设置内容。
public partial class ImpressedContentControl : ContentControl
{
public ImpressedContentControl()
{
InitializeComponent();
}
public Control ControlContent
{
get { return (Control)GetValue(ControlContentProperty); }
set { SetValue(ControlContentProperty, value); }
}
public static readonly DependencyProperty ControlContentProperty =
DependencyProperty.Register("ControlContent", typeof(Control), typeof(ImpressedContentControl), new UIPropertyMetadata(""));
}
我想使用它有点像
<controls:ImpressedContentControl Grid.Column="1">
<ControlContent>
<TextBlock Text="FooBar Text" />
</ControlContent>
</controls:ImpressedContentControl>
是否可以以某种方式使用 ContentControl 的 Content 属性?
有没有更简单的方法来实现这一目标?
任何想法都非常受欢迎!