Superclass/Interface providing TextAlignment and FontFamily properties - c#

I am trying to set properties of several different WPF RichTextBox controls, like List, ListItem, Paragraph. The properties I want to set are, for example, FontFamily and TextAlignment.
I want to do this as generic as possible, so I have one setter for TextAlignment, one setter for FontFamily, and so on, but I cannot find out the common superclass providing all these properties.
Can you tell me what superclass I am looking for, and, if possible, how to find out what superclass provides different properties in general?
Edit: More detailed explanation of the case:
FontFamily is inherited from TextElement, in both Paragraph and ListItem
TextAdjust is inherited from Block in apparently every class but ListItem

You can override metadata for a given UIElement.
For example, if you want to set the default FontSize of all FrameworkElements:
System.Windows.Documents
.TextElement
.FontSizeProperty
.OverrideMetadata(typeof(FrameworkElement), new PropertyMetadata(5));
UIElement/FrameworkElement is as generic as it gets, if you want to apply those defaults to only a few types you need to repeat that line for every type you want.

The common base class that holds all the Font... properties is TextElement, whereas TextAlignment is a property of ListItem.
You can easily find this out by yourself when you browse the MSDN pages. There is a tree view on the left that shows Constructors, Fields, Methods, Properties and Events of a class. If you click one of these items it shows a list of items that are defined in that class.

All the properties you are trying to set are dependency properties. You can use the DependencyObject.SetValue method, e.g.:
DependencyObject x;
x.SetValue(Block.TextAlignmentProperty, TextAlignment.Justify);
It's just a matter of finding the correct dependency property descriptions then.

Related

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.

Change (or Centralise) Property Value for all Controls on a WPF Window

New to WPF so I'm sure it's an easy one this; for every control of a certain type/s, I want to set a property to the same value. The property is telerik:Theming.Theme and I would like to apply the same value to all telerik: controls. If there's no way to do this then how do I use a resource to define this value and refer to the resource in the controls?
Thanks.
You may override the default value of any dependency property for a specific control type (precisely a specific type of dependency object) by DependencyProperty.OverrideMetadata in your application initialization code (e.g. the static constructor of your MainWindow):
Telerik.Windows.Controls.Theming.ThemeProperty.OverrideMetadata(
typeof(SomeControlClass),
new PropertyMetadata(SomeDefaultValue));
where SomeControlClass is the (base) class that you want to give a new default value for the Theme property, and SomeDefaultValue is the new default value.
Note also that Theming.ThemeProperty seems to be obsolete and will be replaced by StyleManager.ThemeProperty.
You do not need to define a resource for binding, but you need to define a property on ModelView and bind it to every control.

How to add properties in PropertyGrid from the database?

I have around 30 elements/objects for which i need PropertyGrid to show their properties in it,but the problem is that every object has different properties so i created a database for it.
I don't know how to add properties in PropertyGrid from the Database.
I am going to assume that you are using Windows Forms, since you are asking about PropertyGrid. If you have objects (meaning classes) that have the properties you want to display in your PropertyGrid, you need only to set PropertyGrid.SelectedObject with the object you want to display. By default, PropertyGrid will use reflection to find all the public properties of your object, and will display them.
You can use various attributes to control how PropertyGrid displays properties. For example, you can apply the Description attribute to a class property to add help text that the property grid will display. You can use the Browsable attribute to control whether PropertyGrid will display a given property. There are other attributes in the System.ComponentModel namespace that you can use.

Italic text in a PropertyGrid

I'm using a PropertyGrid to show custom properties that are exposed through the implementation of ICustomTypeDescriptor.
My objects are setup in a tree structure and values for each property are either set in each object or inherited from parent objects. In the PropertyGrid I want to visually show the user what properties values are set in the selected object, and which are inherited from parent objects.
Right now I am showing every property it two categories. One set shows what the value is set to in the actual object, with a blank field if it not set. The other set shows the property values assigned to the object that are either set in the object, or inherited if not set in the object.
I would like to combine these two groups into one buy showing set properties in regular text, and inherited values in italic text. However, there doesn't seem to be any way to do that through ICustomTypeDescriptor.GetProperties(). And I don't have easy access to the properties of the PropertyGrid since they get created while the program is running.
You can't do italics - but you can do bold; the bold behaviour is determined by the PropertyDescriptor's ShouldSerializeValue; you can wrap PropertyDescriptors via various System.ComponentModel tricks (ICustomTypeDescriptor, TypeConverter or TypeDescriptionProvider) and provide your own PropertyDescriptor.
Alternatively, there are similar grids with more options, such as by VisualHint - see "Property customization" on that page.

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