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

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

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

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

作者:佚名點(diǎn)擊:367次評(píng)論:0次標(biāo)簽: WPF Property

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

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

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

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

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

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

      第三步:通過(guò)WindowsFormsHost使用PropertyGrid控件。 

代碼

      看下效果Button的屬性:

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

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

使用方式很簡(jiǎn)單。由于它是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è)計(jì)器里面的屬性框控件。WF4.0的流程設(shè)計(jì)器有一個(gè)這樣的PropertyGrid控件。我們利用它來(lái)實(shí)現(xiàn)自己的PropertyGrid控件。這也是本文重點(diǎn)介紹的方式。參考:Native WPF 4 PropertyGrid。分五個(gè)步驟去實(shí)現(xiàn)。

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

2、用Workflow Foundation的WorkflowDesigner一個(gè)對(duì)象作為這個(gè)控件的私有成員。

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

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

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

代碼如下:

總結(jié):本文提供了三種方式去在WPF中實(shí)現(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中三種實(shí)現(xiàn)PropertyGrid的方式

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過(guò)難過(guò)
    • 5 囧
    • 3 圍觀圍觀
    • 2 無(wú)聊無(wú)聊

    熱門評(píng)論

    最新評(píng)論

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

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