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
|
Hi, I'm trying to provide a custom editor for all boolean type so that a checkbox is used rather than a combobox. However, I am getting the following binding error: System.Windows.Data Error: 35 : BindingExpression path error: 'Value' property not found on 'object' ''Boolean' (HashCode=1)'. BindingExpression:Path=Value; DataItem='Boolean' (HashCode=1); target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1') Here is the editor template: <ms:TypeEditor EditedType="{x:Type sys:Boolean}"> |
|
|
The type editor mechanism is only for introducing new types. Because the boolean type already has special handling from the grid, what you need to do is restyle the built-in editor using the BuiltInEditorStyles property. There is an example of this in step 5 of the customisation walkthrough sample. One caveat around this is that styling built-in editors happens at the editor level, not the type level, and that by default the same editor (the combo box) is used for both booleans and enums. So restyling the ListSelectEditorKey to be a checkbox would also affect enums. You can get around this using a template selector but it is a bit fiddly I'm afraid. What you need to do is: 1. Create a class derived from DataTemplateSelector. Override the SelectTemplate method to check the property type. The catch is that the "item" parameter passed to SelectTemplate is *not* a bool or enum, but an ObjectWrapper<bool> or ObjectWrapper<SomeEnumType>. So your implementation should cast to ObjectWrapper and look at the Property.PropertyType: public override DataTemplate SelectTemplate(object item, DependencyObject container) 2. In your XAML, create an instance of this selector. (In this example I just reuse the built-in editors but you could use a custom DataTemplate if required.) <local:BooleanOrEnumSelector x:Key="sel" 3. Now create a style to replace the ListSelectEditorKey template. You need to set the Control.Template property to a ContentControl whose content is {Binding} and whose ContentTemplateSelector is the selector you just created. <Style x:Key="listReplacement"> 4. Use the BuiltInEditorStyle mechanism to restyle the ListSelectEditorKey using this style: <ms:PropertyGrid.BuiltInEditorStyles> The upside of this approach is that, by extending the selector, it allows custom presentation for different enum types as well as for booleans; that said, there are a few things we could do to make this simpler and we will keep this issue in mind. |
|
|
Thanks for response. I will implement your solution tomarow as I don't have time tonight. It is a bit of a convoluted approach but at least it only needs to be done once. I am however having the same issue creating a custom editor for the TimeSpan type. It would appear as though there is no custom editor for this type so the above issue should not apply (I am assuming as I could not find a list of built in editors in the documentation). Is this an issue with the way the property grid handles structs? |
|
|
Yes, I'm afraid type editors only work with reference types at the moment. Structs have to be hooked up using property editors. This happens because the type editor mechanism binds the editor template directly to the object, so it needs to have a reference. We plan to rationalise this soon (which will also address your boolean editor issue) but we can't yet commit to a date. There is a list of built-in editors in the docs at Help Topics > Built-In Editors -- hope this helps. |
|
|
I implemented your solution for using a CheckBox for a Boolean type. It worked beatifully, thanks. I decided to try the same method to get around structure types but this time overiding 'ReadOnlyDisplayKey' and providing a custom template selector. The selector in question was never being called in my solution. At this point I decided to simplify the problem by just creating a simple BuiltInEditorStyle for 'ReadOnlyDisplayKey'. It dosn't seem to be overriding the default style. Any thoughts? |
|
|
This happens because the ReadOnlyDisplayKey template is based on a TextBlock, which is not a Control, and therefore doesn't understand the Control.Template property that is being set by the style. I have a candidate fix for this, but there is a broader underlying issue with binding to struct members in WPF which I need to consider how to address. I am also conscious that styling ReadOnlyDisplayKey is a kludge and I would prefer to put the time into a "nice" solution. Anyway, I will aim to get something into the next nightly build and will let you know. |
|
|
I have added experimental support for type editors for structs (including built-in types like booleans and enums) and it will be in the latest nightly build (20080208 at http://www.mindscape.co.nz/Products/WpfPropertyGrid/nightlybuilds.aspx -- should appear in the next 12-18 hours). Please note that this is at an early stage. So you can now do something like this: <ms:PropertyGrid> Or: <ms:TypeEditor EditedType="{x:Type sys:TimeSpan}" Now for the bad news. The CLR doesn't have references to structs; instead, any method or object that wants a copy of a struct gets exactly that, a copy. That includes WPF data templates. So it's not currently possible to create a struct editor that binds to individual members of the struct. You have to create an editor that binds to the entire struct, just as in a property editor (and using the same Value pseudo-property). (This is what you'd expect with TimeSpan anyway, because it's immutable; but you may have some mutable structs you want to manipulate, in which case you need to watch out for this.) So you can't for example do something like the phone number sample with different controls bound to different members of the struct. You should be okay if your structs have TypeConverters (as TimeSpan does) and your DataTemplate operates on a convertible type (in the above example, the text editor operates on strings, and TimeSpanConverter can convert between strings and TimeSpans). Otherwise, I think you will need a control with a bindable property of the appropriate type (in the above example, CheckBox has the IsChecked property which is of boolean type, or near enough). You may be able to do something with value converters within the DataTemplate, but I haven't looked into this. Anyway, I hope this addresses some of the issues you've encountered. As I said, support for struct type editors is currently at an early stage so do let me know if you encounter any issues or if you have any feedback. |
|
|
Thanks for all your help Ivan. I downloaded the nightly build from the link you provided. Using the following: <ms:TypeEditor EditedType="{x:Type sys:TimeSpan}" EditorTemplate="{StaticResource {x:Static ms:PropertyGrid.SimpleTextEditorKey}}" /> I get these binding errors: System.Windows.Data Error: 35 : BindingExpression path error: 'Editable' property not found on 'object' ''TimeSpan' (HashCode=50000000)'. BindingExpression:Path=Editable; DataItem='TimeSpan' (HashCode=50000000); target element is 'TextBox' (Name=''); target property is 'IsEnabled' (type 'Boolean') System.Windows.Data Error: 35 : BindingExpression path error: 'Value' property not found on 'object' ''TimeSpan' (HashCode=50000000)'. BindingExpression:Path=Value; DataItem='TimeSpan' (HashCode=50000000); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String') I have no other structs that I use as properties in the objects I use the property grid to edit. TimeSpan is important for our application though as it is used often. |
|
|
Hmm, that sounds like you have the build before the struct support changes. Looking at the timestamp of your message I think you must have downloaded the nightly just before the new one got pushed out. (It's confusing talking about "nightlies" with the different time zones involved!) Can you check that your build is datestamped "20080228" please? If not, could you re-download and try again please? Thanks! |
|
|
Yes that was exactly the problem. I grabbed the latest build and everything worked perfectly. This is a much more intuitive solution than the template selector. Thanks! |
|