C# propertyGrid miscellaneous questions about properties - c#

I have several questions about properties displayed in propertyGrid i cant seem to find answers for:
1) What do i do if my property is a class? When multiple objects are edited, straaange things happen (when property is struct or simple type, its okay, but if property is class, it passes same class's reference to all edited members when value is applied, which is obviously awful, and it wont recognise two class with identical members, because they have different reference, etc)
2) How do i resize the custom dropdown Editor i created for a property?
3) Can i somehow differentiate between empty value and no value when multiple objects have different values? Like, so
if i have selected 2 objects with same "name" property, it would show name: TEXT
if i have selected 2 objects with "name" = "", it would show name:
if i have selected 2 objects with different "name", it would show name:
4) How do i show something when value is blank? It has to disappear when user starts editing! For example, i have property "name" and if its blank, it should have <> displayed in property grid, but as soon as user clicks to edit, when cursor appears, the <> should disappear

There is a good sample how to implement a custom editor for a property grid in the MSDN:
http://msdn.microsoft.com/en-us/library/ms171840.aspx
and a great article how to realize a custom dropdown, e.g. a dropdown with checklist controls in it on codeproject:
http://www.codeproject.com/Articles/21085/CheckBox-ComboBox-Extending-the-ComboBox-Class-and
If you combine the two articles, you're done ;)
Greetings,

Related

Nest several properties in one expanded property

I have some questions of property grid.
Originally, there are three properties in class "Light" (Light: Light Intensity/Light Intensity(Cadenla)/Intensity(Lumen)).
Now, I want to combine three properties with one property "Light Intensity Unit", the user can select those three in the dropdown list, after selecting, one slider shows up and let the user to edit the chosen property.
Any guidance of how to do that? It looks like something related expandableObjectConverter?
Here is a picture for better shown what I imagine.Picture of what I imagine

Is Enum the only way to show values for a Property as drop down list in the design time

I know when I use Enum and put it as Property for class/UserControl, it will show/display it with drop down list like this:
But is this the only way to display available values like "drop down list"? Do we have other ways to do it?
Edit: The scope is Windows Form Application
For who didn't understand me
In that image(http://i.stack.imgur.com/NMank.png) where wrote "DataBase" is the Name of the Property in design time.
The values: NotChoseYET, ChooseDataBase, ThereAlreadyDataBase are values in Enum I created.
As you can see they displayed in a DropDownList in the design, so I want to ask if there are other ways to display like that without Enum?
Update
After tons of searches, I found what I had answered was only partially correct, so I would like to answer it again.
The answer to your question is a definite NO, we have some other way to show values on property as DropDownList. Like what I mentioned in my old answer, if values come from some kind of Set, or in other word it belongs to a collection of values, it will be displayed as a DropDownList without any extra efforts (because the UITypeEditor has been specified for them, you will understand this point later). Here are 3 examples:
If a Property is a bool, in the designer it will show you a DropDownList contains True and False;
If a Property is a Font.Name, in the designer it will show you a DropDownList with SmallIcon;
if a Property is a Color, in the designer it will show you some DropDownList encapsulated in a TabControl.
From those "native" examples, you may realize a fact: we could somehow use some controls other than a simple DropDownList in the Property Tab during the design time. For example, a Color property gives a TabControl instead of a simple DropDownList, and a Font.Name property gives a customized DropDownList.
Yes, this is the second thing I am going to talk about. You are not only able to customize the items in the DropDownList, but also the View of that Value Choosing Editor.
However, this is very tricky and non-trivial. You are not recommended to do this unless it really adds value to your user control in the design time.
In short, you need to inherit from UITypeEditor, and override 2 functions:
GetEditStyle(), which
indicates to the design environment which kind of user interface your UI type editor implements
and EditValue(), which
establishes the interaction between the design environment and the user interface for editing your custom type
Then for your property which makes use of the Editor, you need to add EditorAttribute to it to specify the Editor used when selecting value of that property.
For better details, you are suggested to check this MSDN walk-through, which explains how to design a customized Value Editor in the design time.
Now back to the point where we left over. The native type, like bool and Color, has already bond to
a UITypeEditor, thus no further working should be done.
Old Answer:
For properties, you need to choose a value from a kind of Set, and all elements in that Set will be displayed as Items in a DropDownList during design time. When you try to define the Set, Enum is one method to define them. Also, you can use set of struct, like Color. In other words, if you have a Property that returns Color (or other structs), during design time it will appear as a drop down list.
You can easily add items to a combobox control by using its .Items property:
private void TestForm_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
// add items to the combobox
// you can use any object. The .ToString() method will be used for displaying it
cbxTest.Items.Add("This is test string " + i);
}
}
This will yield the following form:
I came across this question as I recently needed to do the same.
I posted a question ... which in the end I anwsered myself here.
In a nutshell: Implement a type converter
GetStandardValuesSupported(ITypeDescriptorContext context) to return
true and GetStandardValues(ITypeDescriptorContext context) to return
the StandardValuesCollection for the property type. Finally just
decorate the property like so:
[TypeConverter(typeof(MyClassConverter))]
public MyClass MyProperty { get; set; }
The designer property window will now have a dropdown with valid values.

Why we use Bound field in GridViews

I am pretty shocked no one asked this question before as when I searched I couldn't anything related,
https://stackoverflow.com/search?q=what+is+a+bound+field+c%23
Anyway my question is,
Can someone explain what a bound field is please in easy words and when we use it with example.
Research I did
We use it in a GridView or DataView but why we can't use the default option for displaying data then using Bound Fields.
Well Data Binding in general is the principle of declaratively stating that some user interface element's value will come from some source, and be populated by the runtime rather than the developer manually setting and getting values from controls in codebehind files.
So in WPF, for example, you can set the DataContext property of an entire window to an object, and then for each control on that window say from which properties of that object the WPF runtime should get their value.
For example, for an Employee viewmodel with Forename and Surname properties, you might create an EmployeeView window with two textboxes, where one is "bound" to the Forename property and the other is "bound" to the Surname property. At runtime, the framework will look at the bindings on each control, fetch the value from the data automatically and populate the control's value field. Likewise, when the value in the control is modified by the user, data-binding can push the new value to the data model it is bound to.
This is in contrast to the typical approach in the days of VB6, where setting those textboxes' content would be done in the codebehind of the form (e.g. forenameTextBox.Text = employee.Forename). Data binding in VB6 (and WinForms, for that matter) is different, where the framework does what I described above, but automates getting data from a database in the process. That's fallen out of favour in recent years, though (and for good reason).
The BoundField class is used by data-bound controls (such as GridView
and DetailsView) to display the value of a field as text. The
BoundField object is displayed differently depending on the data-bound
control in which it is used. For example, the GridView control
displays a BoundField object as a column, while the DetailsView
control displays it as a row.
For more visit MSDN Help Bound Field Description

Add tooltip for PropertyGrid

I want to add a tool tip for items in a Property Grid. When the user hovers over a property I want the tooltip to display a hint about the usage of that property. The hint string should be different for each different value of the property — for example if one property is a list of strings each string would have a different hint.
Is this possible?
The PropertyGrid is not very flexible and doesn't expose any of the individual controls on it. You can access the control (textbox or dropdown) that you're looking to show the tooltip on via reflection but that is far from trivial, especially since all the control classes are unique and internal to the property grid.
Using the Description attribute is by far the best value. If your list of strings for that property aren't obvious enough to portray their meaning without providing a tooltip, perhaps you should revisit the string text you are showing for each item in the list.

How do I show like-values in custom fields in a property grid?

I have a property grid that helps me manage all of the controls on a form. These controls are for designer-type folks, so I'm not really worried that much about the user interface... until someone selects multiple objects.
I have a UITypeEditor for the "BottomDiameter" property on these common objects. It keeps track of units (meters vs feet) and does some nice things on-the-fly. However, when someone selects two or three common objects, BottomDiameter is blank, even though it evaluates to the same text string.
The reason (I think) that it is blank is that it is actually three separate objDiameter objects. How can I tell the property grid to behave like all of the other properties and show the value if it evaluates to the same string???
UPDATE: For example, the "Anchor" property has a text output of "Top, Right" but when you pull it down it is an object. Yet, when you select five objects on your form that all have the same Anchor setting you can still see the string "Top, Right" in the property grid.
If your BottomDiameter is a class and not a simple primitive, then you have to override the Equals method in this class.
In the TypeConvertor of the Datatype which is attributed to BottomDiameter Property, you might want to create a vistor like class called say, BottomDiameterVistor which would take an array or a list of the selected BottomDiameter(s). Override the to string property on the BottomDiameterVistor to return your aggregrated text value for the property.

Categories

Resources