Get WPF control properties from the Properties Window - c#

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.

Can you please check How to enumerate all dependency properties of control?
I think this will help you for what you are looking for...
Regards,

Related

Showing public fields of a UserControl in designer

I am trying to add some fields to a custom UserControl that I am making. I have some fields that I like them to be visible in the Properties window of Visual Studio. I tried to use the flags below but I dont see the field in the designer, even after a compile.
How should I do this correctly?
public partial class TosChartControl: UserControl
{
#region PUBLIC FIELDS
[Browsable(true)] //Added this but still does not show up
[Category("Data")]
[DefaultValue(0)]
[Description("ID of the Sensor Node")]
public int NodeId { get; set; }
#endregion
public TosChartControl()
{
InitializeComponent();
}
}
I did clean and rebuild the soloution and projects but I cant still see this field in Properties window. Even restarting the Visualstudio didnt help.
UPDATE: Your public properties are visible in the designer only when it's in another control in the designer. It turns out that you don't need to add this attribute, properties are visible by default in the designer. As far as I understand, when it's in another component's design view, an instance of the user control is created and properties can be shown. Sorry for misleading you in the beginning, I thought it was necessary to add it.
Try this attribute:
[Browsable(true)]
http://msdn.microsoft.com/en-us/library/system.componentmodel.browsableattribute.aspx
To elaborate on henginy's updated answer:
Be sure that you are looking at an instance of the control you want to modify properties for, and not the definition of the control itself.
To clarify, when you add a property to your TosChartControl class, you won't see the property in the TosChartControl.cs [Design] tab, you will see it where you implement a TosChartControl, such as your Form1.cs [Design] tab, e.g. the containing control to which you have added your custom control.
...Assuming that your Properties window is visible, and that you have the control selected.
What to take away from this lesson:
Understanding what the properties window is actually showing you — It's contextual.
The difference between the model and the implementation of the model — e.g. Designing the custom control and designing the form that uses the custom control.

Move controls from the user control to a panel control

I'm trying to write a form theme class library to adjust the form layout in a simple way for any project I'll be working on.
This is basically an idea of what it should look like:
http://www.beaverdistrict.nl/form_layout.png
In essence, the plugin works as follows:
// form class, which inherits the plugin class
class FormToTheme : ThemedForm
{
public FormToTheme()
{
// some code here
}
}
// plugin class itself
class ThemedForm: Form
{
public ThemedForm()
{
// some code here
}
}
Basically I set the FormBorderStyle to None, and drew the layout by code.
But now, the controls that are added can be placed over the custom titlebar, which isn't possible in a normal form if you keep the default FormBorderStyle.
So I figured that I could work around this by automatically adding the controls to the content panel, instead of the usercontrol.
So what I tried to do was this:
private void ThemedForm_ControlAdded(Object sender, ControlEventArgs e)
{
// some simple code to set the control to the current theme I'm using
e.Control.BackColor = Color.FromArgb(66, 66, 66);
e.Control.ForeColor = Color.White;
// the code where I try to place the control in the contentPanel controls array,
// and remove it from it's parent's controls array.
if (e.Control.Name != contentPanel.Name)
{
e.Control.Parent.Controls.Remove(e.Control);
contentPanel.Controls.Add(e.Control);
}
}
But when I try to add a new control in the main form as well as in the visual editor, i get the following error:
child is not a child control of this parent
So my question is: is there a way to work around this error, and move the controls from the usercontrol to the content panel?
Note that I do want this to be automated in the ThemedForm class, instead of calling methods from the main form.
EDIT:
I tried this:
http://forums.asp.net/t/617980.aspx
But that will only cause visual studio to freeze, and then I need to restart.
I know that it is not really appropriate to answer ones own question, however the solution I came up with will take quite some explaining, which will be too much to add in my question with an edit.
So here we go:
Inside the inherited 'ThemedForm' class, I created a private variable, in order to be able to return the variable when the Controls property would be called:
private Controls controls = null;
I set the variable to null, because I need to pass variables to the class in the 'ThemedForm' class constructor. I will create a new instance of the class later on.
Then I created a class to replace the Controls property:
public class Controls
{
private Control contentPanel = null;
private ThemedForm themedform = null;
public Controls(ThemedForm form, Control panel)
{
contentPanel = panel;
themedform = form;
}
public void Add(Control control)
{
if (control != contentPanel)
{
contentPanel.Controls.Add(control);
}
else
{
themedform.Controls_Add(control);
}
}
public void Remove(Control control)
{
if (control != contentPanel)
{
contentPanel.Controls.Remove(control);
}
else
{
themedform.Controls_Remove(control);
}
}
}
I know this class holds far from all functionality of the original Controls property, but for now this will have to do, and if you like, you can add your own functionality.
As you can see in the Add and Remove methods in the Controls class, I try to determine wether the control that needs to be added is either the content panel I want to add the rest of the controls to, or any other control that needs to be added to the content panel.
If the control actually is the content panel, I add or remove it to or from the Controls property of the base class of the 'ThemedForm' class, which is a 'Form' class. Otherwise, I just add the control to the content panel's Controls property.
Then I added the Controls_Add and Controls_Remove methods to the 'ThemedForm' class, in order to be able to add or remove a control from the Controls property of the 'ThemedForm' base class.
public void Controls_Add(Control control)
{
base.Controls.Add(control);
}
public void Controls_Remove(Control control)
{
base.Controls.Remove(control);
}
They are quite self-explanatory.
In order to call the Controls.Add or the Controls.Remove methods from an external class, I needed to add a public property that hid the current Controls property, and returned the private variable that I assigned to the replacing class.
new public Controls Controls
{
get { return controls; }
}
And finally I created a new instance of the Controls class, passing the current 'ThemedForm' class, and the contentPanel control, in order to get it all to run.
_controls = new Controls(this, contentPanel);
After doing all this, I was able to 'redirect' any controls that were added to the UserControl (even inside the visual editor) to the content panel. This allowed me to use the Dock property of any control, and it would dock inside the content panel, instead of over my entire form.
This is still a little bit buggy, because inside the visual editor the docked controls still seem like they are docked over the entire form, but when running the application the result is as I wanted.
I really hope this helps anyone.

Get rid Of Many Events and Properties in user control windows appilication

Hi Experts
I create an user control in windows application.
when it inherits from Control base class it would have many events and properties that may be not uses in usercontrol and I what to hide then in Properties Window.
How I can do this?
thanks
use the following 3 attributes on the events or properties:
when you cannot override the property, just replace 'override' with 'new'. The EditorBrowsable attribute has no effect on the properties window, but on the code editor.
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never)]
public override Color BackColor
{
get
{
//implementattion
}
set
{
//implementation
}
}
If i have understood properly, then you should change the accessor level base control methods and events to private which you want to hide from child class.

Set PropertyGrid Default Popup Editor Startup Size (WinForms)

). How can you set the default size with which the Popup Editor shows up when you invoke it from a Property Grid.
This is for everybody who is familiar with Windows Forms' Property Grid Editor.
You know that if you throw a List property to a Grid, it shows the little [...] button which if you press it pops up its default sub-value editor. I actually use the editor for another type of object, but I gave this example just so you know what I'm referring to. And here's a picture, at least until the link lives:
http://www.perpetuumsoft.de/sf/en/ims/rssSilverlight/GetStart/image032.jpg
My understanding is that (both for modal and non-modal editors) it is completely up to the whim of the control being shown. If the UITypeEditor involved chooses a big form, it will be big...
The only way to change that would be to define your own UITypeEditor and associate it with the types involved (sometimes possible with TypeDescriptor.AddAttributes(...), that creates the same form as the runtime wanted to show, but resizes it before showing.
You can achieve this by inheriting from the standard System.ComponentModel.Design.CollectionEditor and then set the desired size in the CreateCollectionForm override.
Decorate your collection to use the custom collection editor.
Below is an example that will start up the collection editor in full screen
class FullscreenCollectionEditor : System.ComponentModel.Design.CollectionEditor
{
protected override CollectionForm CreateCollectionForm()
{
var editor = base.CreateCollectionForm();
editor.WindowState = System.Windows.Forms.FormWindowState.Maximized;
return editor;
}
public FullscreenCollectionEditor(Type type) : base(type)
{
}
}
And then decorate your collection property with [Editor(typeof(FullscreenCollectionEditor), typeof(UITypeEditor))] i.e.
public class MyModel
{
[Editor(typeof(FullscreenCollectionEditor), typeof(UITypeEditor))]
public List<FileModel> Files { get; set; }
}

Setting custom control properties

I though it would be very simple but I can not get it today.
I have a user control, with a grid control contained in it.
public Unit Width
{
get
{
return CustomerGrid.Width;
}
set
{
CustomerGrid.Width = value;
}
}
I expose the width property and when I set it in the designer it works at run-time but not design time.
What class do I inherit from or method to override to get my controls to function at design time.
Note I tried to inherit from WebControl but got the message
Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class
I understand you're talking about user controls (ascx) and not about custom controls (no ascx). If this is the case, you should inherits from UserControl and you would have the property available on design time without any other addition.
In case you're talink about custom controls, here you have a good article about adding design time support to custom controls
http://msdn.microsoft.com/en-us/library/aa478960.aspx

Categories