User control design time property not showing in properties window - c#

I have a UserControl with a custom property:
public Size TestSize { get; set; }
I would like this property to show up in the properties window during design time of the UserControl, and not only where the UserControl is used as an instance (e.g. when dragged/dropped on a form).
Example:
In the designer of the UserControl one can set properties like "AllowDrop", "AutoScroll", "BackColor" etc. I would like my custom property to show up just the same way in the designer.
I have considered and tried adding attributes to the property like:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
but that does not seem to be the way to go.
Any help is appreciated!

If you really want to show the property in the designer of UserControl1, then the property should belong to its base class. That's because of how designer works
Assuming the root element of the designer is UserControl1, then the properties that you see in properties window, are browsable properties of UserControl1's base class (which is probably a UserControl). To learn more about it, read the second section of the answer.
In most cases you don't need to show those properties in designer, unless you want to derive multiple controls from that base and configure that property in design-time of the derived control, but I assume you know your requirement and now you are able to make a decision base on your requiremets.
This is the way that designer works:
It deserialize the .cs file (and looks for InitializeComponent method, or designer.cs file, also considers the designer-related attributes).
It creates an instance of the base class and add it to the design surface, and applies the deserialized property values. (So the instance that you see in designer, is an instance of the base class. It's a trick. For example, that's why if you have an abstract base class, you cannot have the derived class as root designer without some workarounds.)
It creates the whole control/component trees based on the deserialized code. (So the controls which are on design surface are real instances of the controls)
Of course there are a lot of other things happening, like making the extender providers working, or pre-filtering or post-filtering properties in design-time, but in general it works like what I explained above.
More information / Related Posts
Some other related answers:
A similar question/answer for Form: Custom browsable property for Form at design time
An answer explaining how designer works, also contains an interesting example shows how .cs file which is full of syntax errors can be shown in designer: How does Windows Forms Designer work?

Related

Control's default properties

When I add a control to a form through the designer, not all properties of the control appear in the designer code. For example, when I add a ListBox the UseWaitCursor property does not appear in the designer code unless it is set to True. When I change it to False it disappears from the designer code, which makes me think that the properties somehow have defaults and don't appear in the designer code if left at default.
Can someone please help me understand how the designer works and where all this is tracked. The reason I ask is I am currently writing a class that extends a third party ActiveX control which I plan to initialize dynamically at run time. I was going through the designer code (when the third party control is added through the designer) and a lot of its properties do not appear there.
This is done with the [DefaultValue] attribute. The Control.UseWaitCursor property looks similar to this:
[DefaultValue(false)]
public bool UseWaitCursor
{
// etc..
}
So if you leave the value at False in the Properties window then the designer knows that it should not display the value in Bold and that it is not necessary to put the property assignment in the InitializeComponent() method since the default is already good. An ActiveX control will certainly have a lot of properties set at its default value as well.

Base Page with Layout for Windows Phone 7

I am trying to define a common base page for all of my Windows Phone 7 pages that includes a common place for derived pages to add their markup, similar to content placeholders in ASP.NET.
So far the code samples I have seen all add code behavior, but none show how I can use a base page to do the following:
place the application title in the top left corner
place the company logo in the top right corner
have a dynamic (meaning the user can change it) background image
I can define a custom MyTitleControl to achieve most of the items above, but I would still have to rely on all the page referencing it (rather than deriving from a common base page). Is this even possible in WP7?
The only way to do that with a base page class is to use "loaded" event or better method OnNavigatedTo and change visual tree as needed.
PhoneApplicationPage Class derives from UserControl, so it's visual tree is built using InitializeComponent auto-generated method.
It's generated to read XAML and instantiate controls and set root control as a Content of current user control (in this scenario Page).
So you might use it later (not in ctor) or modify tree after this method.
Another option might be creating Style for page and just applying it to all pages.
You can't. This is a limitation of WP7.
Every WP7 app has a single instance of PhoneApplicationFrame associated with it. Everytime the current page changes, the PhoneApplicationFrame 'Content' property is set to the PhoneApplicationPage. And a PhoneApplicationPage does not support additional PhoneApplicationPages inside.
The only way to accomplish something similar, is to convert all of your pages to UserControls, and ensure there is only one PhoneApplicationPage .
You can inherit from PhoneApplicationPage and give your new class additional properties, a new default Style, a new Template - after all a page is nothing but a UserControl with custom properties.
Then alter your myPage.xaml.cs and myPage.xaml files to inherit from your new class instead of from PhoneApplicationPage.

How to save control property from designer auto-filling? [duplicate]

This must be a FAQ, but I can’t find a duplicate question!
There are lot of different attributes that control what the WinForm Designer does with properties on a custom control, I am never clear on the one I should use in this case.
I am looking for:
Designer does not show property in grid
Designer does not read value of property
Designer does not set property to default value
E.g. Designer behaves as if the property was not there.
Designer does not complain if it has already done one of the above before the attributes were added (hard!)
Background.
The code that is giving me the problem is:
this.eventListControl.FilterSets =
((SystList<FilterSet>)(resources.GetObject("eventListControl.FilterSets")));
The FilterSets property should never have been touched by the winforms designer; it is now not Serializable and MsDev falls over every time a form that used the eventListControl is changed!
I think you can use [Browsable (false)] and [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
The second attribute prevents the property from appearing in InitializeComponent

How to get VS2010 not show useless information in the Methods dropdown?

I have an app with a ton of UI components on the form. As a result, when I drop down the Methods (?) combobox, there are a ton of irrelevant entries.
Is there a setting somewhere that limits the list to actual code constructs (e.g. methods/events/etc...)?
Don't know how to do this with the drop down but maybe an extension would help you out.
You could use something like the VS10x Code Map:
The Solution Navigator also groups items by type rather than alphabetically:
The only setting I can find that affects the "Navigation Bar" is to hide/show, located in Tools/Options/Text Editor/[Your Language]/General at the bottom under the display heading, "Navigation Bar" check box.
Some ideas:
1) You could define all of your event handlers and additional methods in a subclass, then a single method in your parent class to connect everything up to a private member of the subclass type. When in the subclass, only the subclass methods would show up in the navigation bar.
2) You could derive a class from your main form class, setting anything you needed to see in the derived class as protected in the base class (such as the event handlers). This would also effectively filter your navigation bar.
HTH

Possible to make value converter accessible only by my user control? (WPF)

So I've gotten in the habit of using WPF/C# value converters because they are awesome. I usually just have a folder set up for value converters and then access them in whatever xaml files I might need them.
I'm currently developing a user control that will have some value converters that I do not want other classes to be able to access. In other words, value converter exists in the code behind file and should be accessible only from that file and the associated xaml. My first thought was to throw it inside the code behind file as a nested class, but I can't find a way to access the nested class from within the xaml file. Am I going in the right direction or is there something else I should be doing?
I could go the cheap way and just throw this control into its own namespace, but that doesn't really solve my problem.
Any help or guidance is appreciated. Thanks!
Below is roughly what I want:
public partial class MyControl: UserControl
{
//misc code for control
private class MyValueConverter : IMultiValueConverter
{
//conversion functions
}
}
is what I have in mind.
Normally, value converters are accessed from WPF like:
`<local:MyValueConverter x:Key="MyValueConverter" />`
This only works if they are in the same namespace. I cannot get this to work if it is a nested class. I've met my goal of making the valueconverter visible only to this user control, but I cannot figure out for the life of me how to access it from within the xaml.
My problem is accessing this converter from in the xaml.
It is not possible (look here for the reason - 2nd paragraph about requirements)
Your custom class must not be a nested class. Nested classes and the "dot" in their general CLR usage syntax interfere with other WPF and/or XAML features such as attached properties.
You can of course work around that - but that limits you to setting the valueconverter in the code-behind (which is yucky :-))
I'm curious, though, why would you not want other classes to use it?
In your code-behind file, mark the methods that contain your converter code as private just like you would any other methods or properties that you wouldn't want to be visible outside of the class. Remember, the XAML and the code-behind file are (by default) both partial representations of the same class, so anything you declare in your code-behind file, even if private, is still visible to the corresponding XAML file.

Categories