西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁業(yè)內(nèi)動態(tài) 業(yè)內(nèi)資訊 → WPF中三種實現(xiàn)PropertyGrid的方式

WPF中三種實現(xiàn)PropertyGrid的方式

相關(guān)軟件相關(guān)文章發(fā)表評論 來源:本站整理時間:2010/9/2 21:40:25字體大小:A-A+

作者:佚名點擊:367次評論:0次標(biāo)簽: WPF Property

  • 類型:wp棋牌益智游戲大。9.1M語言:中文 評分:.0
  • 標(biāo)簽:
立即下載

  由于WPF中沒有提供PropertyGrid控件,有些業(yè)務(wù)需要此類的控件。這篇文章介紹在WPF中實現(xiàn)PropertyGrid的三種方式,三種方式都是俺平時使用時總結(jié)出來的。

     第一種方式:使用WindowsForm的PropertyGrid控件。

     用過WPF的童鞋都曉得,可以通過WindowsFormsHost將WindowsForm的控件宿主到WPF中使用。很簡單,分為簡單的四步。

     第一步:引用dll:在WPF應(yīng)用程序中引入System.Windows.Forms.dll。

     第二步:引用命名空間:在窗體的.cs代碼中引用此命名空間:using System.Windows.Forms;在XAML中引用此命名空間代碼如下:

      第三步:通過WindowsFormsHost使用PropertyGrid控件!

代碼

      看下效果Button的屬性:

將Button的背景色設(shè)置為紅色:

第二種方式:自定義WPF控件。這里以codeplex上的一個開源控件為例。如果你想知道實現(xiàn)的細(xì)節(jié),可以到http://wpg.codeplex.com/上下載代碼學(xué)習(xí)。

使用方式很簡單。由于它是WPF控件,所以不需要使用WindowsFormsHost。

代碼 <Window x:Class="WPGDemoApp.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpg="clr-namespace:WPG;assembly=WPG"
Title="Window1" Height="300" Width="300">
<DockPanel VerticalAlignment="Stretch" >
<Button DockPanel.Dock="Top" x:Name="btn">button for test</Button>
<wpg:PropertyGrid DockPanel.Dock="Top" Instance="{Binding ElementName=btn}" VerticalAlignment="Stretch" IsEnabled="True"></wpg:PropertyGrid>
</DockPanel>
</Window>

把Button的背景色設(shè)置為紅色:

 

第三種方式:使用WF4.0設(shè)計器里面的屬性框控件。WF4.0的流程設(shè)計器有一個這樣的PropertyGrid控件。我們利用它來實現(xiàn)自己的PropertyGrid控件。這也是本文重點介紹的方式。參考:Native WPF 4 PropertyGrid。分五個步驟去實現(xiàn)。

1、自定義一個用戶控件,這個控件繼承Grid類。grid將包含真正的界面元素。

2、用Workflow Foundation的WorkflowDesigner一個對象作為這個控件的私有成員。

3、對于需要設(shè)計的對象,在grid中添加一個PropertyInspectorView對象的子元素。對外它是一個Grid,其實它的類型是ProperyInspector。

4、通過反射獲取和使用PropertyInspector的一些方法。

5、實現(xiàn)一個SelectedObject屬性,標(biāo)準(zhǔn)的PropertyGrid都有它。用來處理PropertyInspector選擇對象的改變。

代碼如下:

總結(jié):本文提供了三種方式去在WPF中實現(xiàn)PropertyGrid。 

 

代碼 using System.Activities.Presentation;
using System.Activities.Presentation.Model;
using System.Activities.Presentation.View;
using System.Reflection;
using System.Windows.Controls;

namespace System.Windows.Control
{
/// <summary>
/// WPF Native PropertyGrid class, taken from Workflow Foundation Designer
/// </summary>
public class WpfPropertyGrid : Grid
{
#region Private fields
private WorkflowDesigner Designer;
private MethodInfo RefreshMethod;
private MethodInfo OnSelectionChangedMethod;
private TextBlock SelectionTypeLabel;
private object TheSelectedObject = null;
#endregion

#region Public properties
/// <summary>
/// Get or sets the selected object. Can be null.
/// </summary>
public object SelectedObject
{
get
{
return this.TheSelectedObject;
}
set
{
this.TheSelectedObject = value;

if (value != null)
{
var context = new EditingContext();
var mtm = new ModelTreeManager(context);
mtm.Load(value);
var selection = Selection.Select(context, mtm.Root);

OnSelectionChangedMethod.Invoke(Designer.PropertyInspectorView, new object[] { selection });
this.SelectionTypeLabel.Text = value.GetType().Name;
}
else
{
OnSelectionChangedMethod.Invoke(Designer.PropertyInspectorView, new object[] { null });
this.SelectionTypeLabel.Text = string.Empty;
}
}
}

/// <summary>
/// XAML information with PropertyGrid's font and color information
/// </summary>
/// <seealso>Documentation for WorkflowDesigner.PropertyInspectorFontAndColorData</seealso>
public string FontAndColorData
{
set
{
Designer.PropertyInspectorFontAndColorData = value;
}
}
#endregion

/// <summary>
/// Default constructor, creates a hidden designer view and a property inspector
/// </summary>
public WpfPropertyGrid()
{
this.Designer = new WorkflowDesigner();

var inspector = Designer.PropertyInspectorView;
Type inspectorType = inspector.GetType();

inspector.Visibility = Visibility.Visible;
this.Children.Add(inspector);

var methods = inspectorType.GetMethods(Reflection.BindingFlags.Public | Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance |
Reflection.BindingFlags.DeclaredOnly);

this.RefreshMethod = inspectorType.GetMethod("RefreshPropertyList",
Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance | Reflection.BindingFlags.DeclaredOnly);
this.OnSelectionChangedMethod = inspectorType.GetMethod("OnSelectionChanged",
Reflection.BindingFlags.Public | Reflection.BindingFlags.Instance | Reflection.BindingFlags.DeclaredOnly);
this.SelectionTypeLabel = inspectorType.GetMethod("get_SelectionTypeLabel",
Reflection.BindingFlags.Public | Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance |
Reflection.BindingFlags.DeclaredOnly).Invoke(inspector, new object[0]) as TextBlock;

this.SelectionTypeLabel.Text = string.Empty;
}

/// <summary>
/// Updates the PropertyGrid's properties
/// </summary>
public void RefreshPropertyList()
{
RefreshMethod.Invoke(Designer.PropertyInspectorView, new object[] { false });
}
}
}

效果:

 總結(jié):介紹了WPF中三種實現(xiàn)PropertyGrid的方式

    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)