110,545
社区成员
发帖
与我相关
我的任务
分享
<UserControl x:Class="AFIS.EMS.SimpleWPF.UserControls.ContentTemplateUC"
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:CustomPage="clr-namespace:AFIS.EMS.SimpleWPF.UserControls"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="300"/>
<RowDefinition Height="5"/>
<RowDefinition Height="*"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<GroupBox Grid.Column="0" Header="未设置" Margin="5,0,5,0" BorderBrush="LightBlue" BorderThickness="1">
</GroupBox>
<DockPanel Grid.Row="2">
<DataGrid HorizontalAlignment="Stretch" Margin="5,0,5,0">
<DataGrid.Columns>
<DataGridCheckBoxColumn/>
<DataGridTextColumn Header="ID" />
<DataGridTextColumn Header="CPU" />
<DataGridTextColumn Header="主频" />
<DataGridTextColumn Header="测试字段1" />
<DataGridTextColumn Header="测试字段2" />
<DataGridTextColumn Header="测试字段3" />
<DataGridTextColumn Header="测试字段4" />
</DataGrid.Columns>
</DataGrid>
</DockPanel>
<CustomPage:PageUC Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>
</Grid>
</UserControl>
#region 引用
using System;
using System.Windows.Forms;
using AFIS.EMS.SimpleWPF.Client.UserControls;
#endregion
namespace AFIS.EMS.SimpleWPF.Client.Views
{
public partial class ContentTemplateForm : Form
{
public ContentTemplateForm()
{
InitializeComponent();
this.elementHostTreeView.Child = new LeftTreeViewUC();
this.elementHostRight.Child = new ServerUC();
}
}
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/当前命名空间的名称;component/Themes/BasePage.Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
另外对于你顶楼给的代码,需要修改很简单,将<UserControl></UserControl>标签内部的代码复制到我所给示例代码的<ControlTemplate TargetType="{x:Type local:BasePage}">标签内部,然后找你需要添加添加内容的位置插入标签<ContentPresenter />即可,但只能插入一处。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type local:BasePage}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:BasePage}">
<Grid Background="White" Opacity="0.8">
<Grid Name="ToolbarGrid" Height="48" VerticalAlignment="Top" >
<StackPanel Name="Toolbar" Margin="6,0,0,0" Orientation="Horizontal" />
</Grid>
<Grid Name="MainGrid" Margin="0,48,0,0">
<ContentPresenter />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
public class BasePage : System.Windows.Controls.UserControl
{
static BasePage()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(BasePage), new FrameworkPropertyMetadata(typeof(BasePage)));
}
private static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(BasePage), new FrameworkPropertyMetadata(string.Empty));
public string Title
{
get { return GetValue(TitleProperty).ToString(); }
set { SetValue(TitleProperty, value); }
}
}
这里定义了一个叫BasePage的UserControl,用来替代系统的Page,然后自己添加了一个叫Title的属性。布局样式通过ResourceDictionary来指定,其中<ContentPresenter />部分就是留给派生类填充的,当一个派生类继承这个BasePage后,所有设计的xmal代码自动填充<ContentPresenter />的位置,同时共享父类所有资源。