This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello Ivan! I have a question. My project includes functionallity that add nodes dynamically. Some of them are group headers. (PropertyNode class members). Question: How can I set their background? |
|
|
You will need to style the grid. Depending on your exact requirement this may just be a matter of creating a GroupStyle and supplying the appropriate GroupDescription, or you may need to style the cells themselves. Check out the "Whalesong" and "Poppy" examples in the Templating sample -- they don't do exactly what they want but they should give you a few pointers. |
|
|
Hello Ivan. It's again me. I already implemented your WPFPropertyGrid Control, and made its functionallity. It' s working good. But there are two last problems. I've already posted about them on your forum, but couldn't solve it. Please, copy and try to run code from TestPropGrid.cs. (it is placed in archive TestPropGrid.rar) You see, if I bind class Property to PropertyGrid using code: TestGrid.SelectedObject = new Property(); in the window I see PropertyGrid with expanded nodes, and headers of groups are the same colour as PropertyGrid Background. But as You know I add nodes at runtime. And result, as you can see are next: nodes collapsed, and group headers background is white. How can I expand that nodes and make group headers background the same colour as the background of PropertyGrid. Result described by picture PropGrid.gif 1) You told me to style nodes. I've created Style class, necessary Setters, but Nodes and PropertyNodes don't have such property as Style. So I couldn't define for them Style. Also I tried to define TargetType attribute for the Style object and set it as Node or PropertyNode. But it must be type, derived from FrameworkElementFactory. Can I set background in another way? 2) Also about Nodes expanding. Properties which are drawn in PropertyGrid I can change also in other moduls of my program. But all this time I want to see how they change in PropertyGrid. But after changing properties I must redraw tree and it collapses.That is problem. (I've seen, when I bound Property class to PropertyGrid, tree is expanded, but when I add nodes at runtime, they collapse). How can I solve it? Or it's not available? If it's not available for me, could you add this functionallity. |
|
|
Picture with problems |
|
|
Hi Mykhaylo, I'm going to answer this in two parts. I'll deal with your second question first. We don't currently provide a programmatic way to expand nodes. We've noted it as a possible feature. In the meantime, you can add this feature by subclassing the grid and reaching into the underlying TreeListView: public class ExpandableGrid : PropertyGrid You can see how to extend this to expand every level of the tree if required. I have omitted error handling here. Specifically this will give you a NullReferenceException if you call it before the template has been applied, e.g. in your window constructor. (And of course it won't work if you have retemplated the grid so it doesn't use a TreeListView, or doesn't use the standard part name.) You could call it from an OnApplyTemplate override, and of course you can safely call it from the parts of your program that are adding nodes after the window has loaded and the grid has been shown. |
|
|
Okay, on to the grouping question. There are two possibilities here: 1. Add the nodes at the top level, and use grouping to group them. 2. Add a parent node, and add child nodes to that, but have the parent node displayed in the same style as group headers. If the grouping is purely visual, you probably want to take approach 1. If there is actual data associated with the parent, approach 2 makes sense. Here's how to go about them. 1. Custom grouping In this case, instead of adding a parent node to the grid, and adding the child nodes as children of that node, you just add the "child" nodes directly to the grid. Now you need to tell the grid how to group them. As you know, you do this by adding a PropertyGroupDescription with a property of "Node" and a suitable IValueConverter. The logic of that IValueConverter will depend on your application, but let's imagine that you want to group the nodes by source, so that all properties of Person A will appear together, and all properties of Person B will appear together. So your IValueConverter will take a node, and return something like ((Person)(node.Source)).Name. See http://www.mindscape.co.nz/blog/index.php/2008/01/27/sorting-and-grouping-in-the-wpf-property-grid/ for more information and examples. This is written in terms of ItemsSource and SelectedObject but applies equally to runtime-added nodes. 2. Making parent nodes look like group headers This is a bit more complicated because we need to style the grid (not the nodes). By doing this, we can insert our own template for drawing the property names, and can make them look like group headers if we wish. The markup for doing this is fairly routine, but there's a lot of it, so I've attached it as a separate file, and will discuss only the high level salient points: * RuntimeNodesStyle re-templates the grid. The result looks like the original, so why do we bother with this? Because it allows us to reference our own PropertyNameCellTemplate, instead of the one used internally by the default template. For more info and examples, see the "Templating" sample (this was based on "Poppy" with a more sober group style). * TreeListViewStyleExpander and all the little converters support this "making it look like the original" by replicating layout etc. * PropertyNameCellTemplate is where the action happens. Again, most of this is devoted to looking like the default template, but check out that second DataTrigger. This says: if the node has child items, display it in bold text on a grey background. Just like the group style (in fact we could probably extract these into a common style). So now parent nodes will look like group headers (even though they're not really). You can apply this style declaratively in XAML (<ms:PropertyGrid Style="{StaticResource RuntimeNodesStyle}"...) or through code (TestGrid.Style = (Style)FindResource("RuntimeNodesStyle")...). Hope this makes sense -- the markup is a bit fiddly but there's nothing too complicated in there. |
|