我正在尝试为Windows桌面应用程序设计精美的WPF控件。 我想知道是否有可能在WPF控件中公开嵌入式面板。 这个想法是创建一个带有标题功能区的面板。将使用一个按钮来部署或隐藏所包含的控件。
这是我的新面板:
<UserControl x:Class="TestRibbonPanel.RibbonPanel.XamlRibbonPanel"
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:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:local="clr-namespace:TestRibbonPanel.RibbonPanel"
mc:Ignorable="d"
d:DesignHeight="150" d:DesignWidth="200" MinHeight="40" x:Name="uc">
<UserControl.Resources>
<local:BorderHeightConverter x:Key="BhConverter"/>
</UserControl.Resources>
<Grid Background="Transparent">
<Grid x:Name="grd" Height="20" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0" x:Name="btn" Background="#333333" Click="Btn_Click"/>
<TextBlock Grid.Column="1" Grid.Row="0" Text="Title" Background="#333333" Foreground="#CCCCCC"/>
</Grid>
<Popup x:Name="pu" StaysOpen="True" PlacementTarget="{Binding ElementName=grd}" AllowsTransparency="True" PopupAnimation="Slide" IsOpen="True">
<Border x:Name="brd" Background="White" CornerRadius="0,0,10,10" BorderThickness="2" BorderBrush="Black" Height="{Binding Path=ActualHeight, ElementName=uc, Converter={StaticResource BhConverter}, FallbackValue=130}" Width="{Binding ActualWidth, ElementName=grd, FallbackValue=200}" SizeChanged="Brd_SizeChanged">
<WindowsFormsHost>
<wf:Panel x:Name="pnl" BorderStyle="FixedSingle" BackColor="Red"/>
</WindowsFormsHost>
</Border>
</Popup>
</Grid>
在后面的代码中,我使用System.Windows.Controls.Panel作为名为ContentPanel的属性:
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace TestRibbonPanel.RibbonPanel
{
/// <summary>
/// Logique d'interaction pour XamlRibbonPanel.xaml
/// </summary>
public partial class XamlRibbonPanel : UserControl
{
public static readonly DependencyProperty IsOPenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(XamlRibbonPanel), new PropertyMetadata(true));
public bool IsOPen
{
get { return (bool)GetValue(IsOPenProperty); }
set
{
SetValue(IsOPenProperty, value);
}
}
public System.Windows.Forms.Panel ContentPanel
{
get { return pnl; }
}
public XamlRibbonPanel()
{
InitializeComponent();
}
private void Brd_SizeChanged(object sender, SizeChangedEventArgs e)
{
pnl.Size = new System.Drawing.Size((int)brd.ActualWidth, (int)brd.ActualHeight);
}
private void Btn_Click(object sender, RoutedEventArgs e)
{
pu.IsOpen = !pu.IsOpen;
}
}
public class BorderHeightConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (double)value - 20;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
现在,我使用名为EH的ElementHost集成类将WPF控件放入Windows窗体包装器中,该类嵌入XamlRibbonPanel控件。
[DesignerAttribute(typeof(XRibbonPanelDesigner))]
public class XRibbonPanel : UserControl
{
private System.ComponentModel.IContainer _components = null;
private System.Windows.Forms.Integration.ElementHost EH;
private RibbonPanel.XamlRibbonPanel _xp = null;
private Panel _contentPanel = null;
public Panel ContentPanel
{
get { return _contentPanel; }
}
/// <summary>
/// .ctor
/// </summary>
public XRibbonPanel()
{
InitializeComponent();
_contentPanel = xp.ContentPanel;
}
private void InitializeComponent()
{
this.EH = new System.Windows.Forms.Integration.ElementHost();
this._xp = new TestRibbonPanel.RibbonPanel.XamlRibbonPanel();
this.SuspendLayout();
//
// EH
//
this.EH.Dock = System.Windows.Forms.DockStyle.Fill;
this.EH.Location = new System.Drawing.Point(0, 0);
this.EH.Name = "EH";
this.EH.Size = new System.Drawing.Size(150, 150);
this.EH.TabIndex = 0;
this.EH.Text = "";
this.EH.Child = _xp;
//
// XRibbonPanel
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.EH);
this.Name = "XRibbonPanel";
this.ResumeLayout(false);
}
protected override void Dispose(bool disposing)
{
if (disposing && (_components != null))
{
_components.Dispose();
}
EH.Dispose();
base.Dispose(disposing);
}
}
如您所见,我为该控件使用了一个定制设计器,希望将控件公开给设计。这是我在这里已阅读并成功应用于其他“经典”控件的一种技术:
using System;
using System.ComponentModel;
using System.Windows.Forms.Design;
namespace TestRibbonPanel
{
public class XRibbonPanelDesigner : ScrollableControlDesigner
{
XRibbonPanel _panel;
public override void Initialize(IComponent component)
{
base.Initialize(component);
_panel = (XRibbonPanel)component;
if (_panel == null)
throw new ArgumentException();
else
{
System.Windows.Forms.Panel pnl = _panel.ContentPanel;
if (pnl != null)
{
bool result = EnableDesignMode(pnl, "Layout");
}
}
}
}
}
当我将XRibbonPanel放在窗体上时,该面板将按照需要进行操作(打开和关闭面板)。但是不幸的是,我看不到我通常用来放置嵌套控件的“布局”对象。 另一个烦人的效果是,当我移动表单时,弹出的Xaml控件浮动在桌面上,与原始表单分离。例如,当我将表单选项卡保持打开状态时,也会在设计模式下发生。
如果我在上设置了断点:
bool result = EnableDesignMode(pnl, "Layout");
在设计器中,pnl不为null,布尔结果为true。
有任何线索吗?