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, Please find attached a repro project. On the left you have a listview with different axis. On the right you have the min max values for the selected axis. When running the application from visual studio and looking at the output you will notice a binding error being generated when switching between the different items in the listview. It seems connected to the DoubleTextBox control. If I switch it to a regular TextBox the binding error disappears. I haven't been able to eliminate the error. Could you please take a look and see if there is something I'm missing? Thanks Oscar Here is a bit of it binding error generated: System.Windows.Data Error: 6 : 'ObjectSourceConverter' converter failed to convert value '' (type 'String'); fallback value will be used, if available. BindingExpression:Path=Min; DataItem='VmAxis' (HashCode=40060369); target element is 'DoubleTextBox' (Name=''); target property is 'Value' (type 'Double') Exception:'System.Exception: is not a valid value for Double. ---> System.FormatException: Input string was not in a correct format. at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt) at System.Double.Parse(String s, NumberStyles style, IFormatProvider provider) at System.ComponentModel.DoubleConverter.FromString(String value, NumberFormatInfo formatInfo) at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) --- End of inner exception stack trace --- at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward) at MS.Internal.Data.ObjectSourceConverter.Convert(Object o, Type type, Object parameter, CultureInfo culture) at System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter converter, Object value, Type targetType, Object parameter, CultureInfo culture)' A first chance exception of type 'System.FormatException' occurred in mscorlib.dll A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll A first chance exception of type 'System.FormatException' occurred in mscorlib.dll A first chance exception of type 'System.Exception' occurred in System.dll |
|
|
Hi Oscar, Thanks for the repro project. This issue is caused by the two different types involved in the binding - double, and object. TextBox wouldn't have this issue as it automatically converts objects to strings. Would you be able to change your Min and Max properties on the view-model to be double values instead of object? I tried this in your repro project and it worked well. Their logic can check if the AxisProperty properties are really double values or not, and convert them if necessary. Another option would be to make a converter to convert between object and double in whatever way you need, and use it in the binding. A converter in this case has the potential downside of changing the type of the source values to double, regardless of what it started as, due to it being a two-way binding. Let me know if you have any questions. -Jason Fauchelle |
|
|
Hi Jason, Unfortunately we cannot change the view-model Max/Min to doubles as they are bound to the MS ChartAxis which are objects. However I did further investigation and found something interesting. I added a converter (i.e. ObjectToDoubleConverter) as you suggested and put a breakpoint. I noticed that the converter was being called when I was selecting the y-Axis item on the listbox which has the Min/Max as string objects. Why would it call this converter if the DataTemplate for y-Axis has no DoubleTextBox it just has a TextBlock. (see x:Key="AxisPropertyStringTypeTemplate). The DataTemplated is chosen based on a DataTrigger of the property AxisType. I modified the previous solution (see attachment) and introduced a derived class VmAxisString : VmAxis and added an extra item derived from this class to the listbox. The VmAxisString class has Min/Max which are objects of string type. This new item will automatically pick up the DataTemplate which has Switching back and forth between one of the double object items (X-Axis and Z-Axis) and this new item (String-Axis Class) does not generate the binding error. But it does if we select the Y-Axis item. So it does not seem like binding to the two different object types is the real problem. However this then led me to discovered that if the StaticResource is changed to DynamicResource the binding error no longer happens. (i.e. Change Value="{StaticResource AxisPropertyStringTypeTemplate}" and Value="{StaticResource AxisPropertyTemplate}" to Value="{DynamicResource AxisPropertyStringTypeTemplate}" and Value="{DynamicResource AxisPropertyTemplate}" ) You will need to do this change in the attached solution. I also noticed that when using the StaticResource the converter ObjectToDoubleConverter was being called twice. However when I changed it to DynamicResource the converter is called only once. What is your take on this? I'm not sure I understand the reason but it seems like the ContentControl for some reason hangs on to the previous ContentTemplate and tries to bind to it before changing it to the different one. What do you think of changing the StaticResource to the DynamicResource as a solution? Do you see any drawbacks? Thanks Oscar |
|
|
Hi Oscar, Using dynamic resources means the resource will be reloaded every time it is needed which can impact performance. Since you're only dealing with a single very simple dynamic resource, this won't be a problem - so you can use this as a solution if you like. Another option may be to use a template selector instead of using triggers to change the template. A template selector is the recommended approach in the setup you have in your repro app. Since the template is being switched based on a property of the selected object, the existing template will be used for the new selected object before it checks the property and triggers the template change. Note I have not tried this in your repro app, and it may not reflect how your actual app works. Also, in your repro app, the converter simply returns the value without actually trying to convert it into a double value. If this was implemented, maybe the new error you're seeing won't occur? I've tried this in a very simple way and it seemed to help. Regardless, it's great that you have a solution now. -Jason Fauchelle |
|