Tuesday, August 20, 2013

Value produced by BindingExpression is not valid for target property – WPF-MVVM-Prism and Telerik control



Ahhh, Telerik - what a pain this is…Telerik controls have made my life bit hard…
I am using MaskedTextInput control and trying to Clear the text in it on the click on Clear button which is inbuilt in this control. It clears the text however does not clear the value in property bind to it in XAML. To understand what I mean here refer to following tag –
<ams:AmsMaskedTextInput Value="{Binding TemperatureValue,Mode=TwoWay, NotifyOnSourceUpdated=True,ValidatesOnDataErrors=True}" Mask="###.#" Placeholder=" " Width="150" HorizontalAlignment="Left" /> 

Here if I click on the default clear button available in masked text input, it clears the entered text however value entered by user persisted in TemperatureValue  property. So my aim to make the binding property set to null on click of default clear button available in masked input text control.
I was using the MVVM-Prism framework an hence I defined my command in view model as –
Microsoft.Practices.Prism.Commands.DelegateCommand TemperatureClearCommand { get; private set; } 
 
In initialize view model – TemperatureClearCommand = new Microsoft.Practices.Prism.Commands.DelegateCommand (OnMaskedTextInputClear);
And in callback method –
public void OnMaskedTextInputClear()
        {
                TemperatureValue = null;
                NotifyPropertyChanged(() => TemperatureValue);
        } 

Updated the XAML as follows –
<ams:AmsMaskedTextInput Value="{Binding TemperatureValue,Mode=TwoWay, NotifyOnSourceUpdated=True,ValidatesOnDataErrors=True}" Mask="###.#" Placeholder=" " Width="150" HorizontalAlignment="Left"
ClearCommand="{Binding TemperatureClearCommand}"/>

When I ran the application, the associated command method was not getting called on click of Clear button. I received following error in output window – “Value produced by BindingExpression is not valid for target property.; Value=”Microsoft.Practices.Prism.Commands.DelegateCommand”.
This is clear that the delegate command class used by Prism framework is different from DelegateCommand class used by Telerik framework. Therefore here we need to change the type of command we have used as follows –
Telerik.Windows.Controls.DelegateCommand TemperatureClearCommand { get; private set; }

In initialize view model –
TemperatureClearCommand = new Telerik.Windows.Controls.DelegateCommand (OnMaskedTextInputClear); 

And everything worked fine. Hope above way of solving this error helps you.
Cheers…
Happy Teleriking!!!

No comments:

Post a Comment