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
|
How can you programmatically select a given property displayed in the property grid and at the same time make it available for editing (as if user had selected the property with the mouse) If the following is any easier to achieve then please advise how you could select the first property displayed in the grid programatically (assumed to be displayed in a textbox editor) and make it available for editing. |
|
|
Ignoring error handling: PropertyGridRow row = PropertyGrid1.BindingView.First(r => r.Node.Name == myProperty); Note that some controls don't seem to respond very well to having keyboard focus moved onto them programmatically -- this code worked fine with text box controls, but not with calendar controls. Your message indicates you're able to assume text box editors, but you may need to do some more probing if you need to extend it to other editor controls. |
|
|
Thanks for the above. For some reason it doesn't appear to work if I call the code immediately after calling PropertyGrid.SelectedObject = ... Could this be because some of the rendering is done asynchronously ? (it works OK if there is a delay before calling the code) Is there any chance you could add a method to the PropertyGrid control such as - SelectProperty(propName As String, Optional editing As Boolean = False) where propName is the name of the property to be selected and the editor is in edit mode if editing is set to True. This would be a very useful enhacement that would benefit most users. |
|
|
Yes, the way that the grid gets populated is through data binding, so you won't be able to get at the TreeViewItems until binding completes (which WPF does asynchronously). The fix for this is to use Dispatcher.Invoke, with a DispatcherPriority of ApplicationIdle. (Any priority lower than DataBinding would do, but ApplicationIdle is convenient.) Thanks for the suggestion of adding a method to select and edit a specific property. I've logged a feature request for this but can't promise anything at the moment. |
|
|
Thanks. I've got that working now. Just one more related question. Is there a way of programtically entering edit mode after selecting the property and better still of selecting any text displayed (you can assume that a texbox editor is being used for the property) |
|
|
In the tests I did, the code I posted earlier did go into "edit mode" (i.e. it set focus to the text box and selected all the text, so that when I started typing the existing property value was overwritten). Are you seeing something different? If so, it's possible this is another timing issue... |
|
|
I tried this with WPF-Elements 3.0 without success, var currentIndex = propGrid.BindingView.IndexOf(propGrid.SelectedGridItem); // (propGrid is my PropertyGrid) With 2.0 the same code was working (except the minor problem that the Node was focussed but the editor within did not get KeyboardFocus). Has there been any significant API-change s.t. I will need to solve this differently? What I really want is to allow the user to tab through the fields, forward with tab, backward with Shift+Tab, instead of jumping out of the propertyGrid.
|
|
|
I can't think of any changes between Property Grid 2.0 and Elements 3.0 which would cause the behaviour of your code to change, and when I test your line of code I get a non-zero currentIndex (assuming, of course, that the SelectedGridItem is something other than the first item). Can you provide a repro project? |
|
|
Hi Ivan, thanks for your help, I finally found out the problem has nothing to do with the version upgrade. I did get the right index in the first iteratiion, but then setting the SelectedGridItem failed, maybe due to that async Dispatcher-stuff. After setting the SelectedGridItem to the next PropertyGridRow it appears to be null. Then the next call lets the cursor jump to first row. I tried various things calling Dispatcher.Invoke, but without any effect. The only thing that worked out is something like this:
propGrid.SetValue(PropertyGrid.SelectedGridItemProperty, nextItem); Would be really great if you put this Row-Selection-Feature in some of the next releases, s.th. your users don't need to try such nasty things. Best regards, Simon |
|
|
Works, but... Once more I need to bug you, now tabbing through nodes in the main tree works, but when I'm in a property-row in a expanded node within some complex type property, the following snipped gets no valid index but -1. var currentIndex = this.BindingView.IndexOf(this.SelectedGridItem); This is obviously because the SelectedGridItem (not null) is not in the BindingView but in some other view. Could you give me a hint, how I get this a little more general, i.e. I want to get the next item from the current view, something like this:
var currentIndex = BindingView.IndexOf(this.SelectedGridItem); if (currentIndex < 0) { // seems to be child view foreach (var childView in this.BindingView.Where(x=>!x.IsLeaf)) { if (this.SelectNextRowInChildView(childView, shift)) { return true; } } ... I'm stuck now, because I found the PropertyGridRow, which has the SelectedGridItem in its Children-Collection, but I can n o longer get the TreeView associated with it, so I can Set the SelectedGridItemProperty the the next row, but not get the TreeViewItem to do the MoveFocus var tv = (TreeView)(this.Template.FindName("PART_Grid", this)); will probably only work for treeViewItems on root level, right? |
|
|
What I think you will need to do is recurse down the TreeView using ItemContainerGenerator.ContainerFromItem. To expand on this a bit, it sounds like you have found the chain of PropertyGridRow objects from the top level down to the selected item. And you also have the TreeView corresponding to the top level. So now you go something like (note here rowChain is assumed to be top level first and target node last): HeaderedItemsControl c = tv; I have not tested this code, so it may need some tweaking, but hopefully it will set you on the right road. |
|
|
Sounds reasonable, I think that was exactly the missing piece in my puzzle. I will give it a try... |
|