从属性窗口获取 WPF 控件属性 [英] Get WPF control properties from the Properties Window

查看:88
本文介绍了从属性窗口获取 WPF 控件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在选择 WPF 控件时访问属性窗口显示的属性.

问题是,虽然我已经设法在属性窗口中添加了我自己的内容,但我还没有找到一种方法来获取对 WPF 设计器用来显示控件属性的内容的引用.

私有IVsWindowFrame _frame;...if(_frame == null) {var shell = parent.GetVsService(typeof(SVsUIShell)) as IVsUIShell;如果(壳!= null){var guidPropertyBrowser = new Guid(ToolWindowGuids.PropertyBrowser);shell.FindToolWindow((uint) __VSFINDTOOLWIN.FTW_fFindFirst, ref guidPropertyBrowser, out _frame);}}

如您所见,我已经有了对属性"窗口的引用,但不幸的是,我不知道如何列出这些属性.

如果相关,我尝试这样做的原因是因为我想删除(或隐藏)为设计器中的 WPF 控件显示的某些属性.

解决方案

WPF 控件的设计时支持基于公共属性和特性.控件的任何公共属性都显示在属性窗口中,但您可以通过属性更改可见性.隐藏现有属性有一个简单的技巧.您必须定义具有相同名称的新属性并添加属性.是否将属性定义为虚拟,您可以简单地覆盖,但您可以使用关键字 new.

示例代码:

公共部分类 MyUserControl : UserControl{公共 MyUserControl(){初始化组件();}[System.ComponentModel.Browsable(false)]公共新画笔背景{得到 { 返回 base.Background;}设置 { base.Background = value;}}}

<块引用>

设计时属性和继承

当您从具有设计时属性的基组件派生组件或控件时,您的组件将继承基类的设计时功能.如果基本功能足以满足您的目的,则不必重新应用这些属性.但是,您可以覆盖相同类型的属性或将附加属性应用于派生组件.以下代码片段显示了一个自定义控件,该控件通过覆盖基类中应用的 BrowsableAttribute 特性来覆盖从 Control 继承的 Text 属性.

请参阅 MSDN,您必须使用 BrowsableAttribute.基本概念适用于 WinFors 和 WebForms,但 WPF 控件具有相同的功能.

I'm trying to get access to the properties shown by the properties window when a WPF control is selected.

The problem is that although I've managed to add my own content in the properties window, I have not found a way to obtain a reference to the one used by the WPF designer to display control properties.

private IVsWindowFrame _frame;
...

if(_frame == null) {
    var shell = parent.GetVsService(typeof(SVsUIShell)) as IVsUIShell;
    if(shell != null) {
        var guidPropertyBrowser = new Guid(ToolWindowGuids.PropertyBrowser);
        shell.FindToolWindow(
            (uint) __VSFINDTOOLWIN.FTW_fFindFirst, ref guidPropertyBrowser, out _frame
        );
    }
}

As you can see I already have a reference to the Properties Window but unfortunately I have no idea how to get the properties listed.

In case it's relevant the reason I'm trying to do this is because I want to remove(or hide) some properties shown for the WPF controls in the designer.

解决方案

Design-time support for WPF controls is based on public properties and attributes. Any public property of control is shown in properties window, but you may change visibility by attributes. There is a simple trick for hide existing property. You must define new property vith same name and add attributes. Is property is defined as virtual you could simply override but you could use keyword new.

Sample code:

public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
    }

    [System.ComponentModel.Browsable(false)]
    public new Brush Background
    {
        get { return base.Background; }
        set { base.Background = value; }
    }
}

Design-Time Attributes and Inheritance

When you derive a component or control from a base component that has design-time attributes, your component inherits the design-time functionality of the base class. If the base functionality is adequate for your purposes, you do not have to reapply the attributes. However, you can override attributes of the same type or apply additional attributes to the derived component. The following code fragment shows a custom control that overrides the Text property inherited from Control by overriding the BrowsableAttribute attribute applied in the base class.

See MSDN, you have to use BrowsableAttribute. Base concept is for WinFors and WebForms, but WPF controls have the same.

这篇关于从属性窗口获取 WPF 控件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆