frG
frG

Reputation: 23

Silverlight DataContext binding error when property is null

In my Silverlight application i a have TabControl, the tab control has the following item:

<controls:TabItem Header="{Binding Localization.Shift1}" Style="{StaticResource     PanelBarTabItemStyle}" IsTabStop="False">
    <local:DienstDetailItemControl DataContext="{Binding Shift1, Mode=TwoWay, Converter=    {StaticResource DebugConverter}}"/>
</controls:TabItem>

When my application starts the Shift1 property is still null (it will be filled when the user selects an option).

I get the following error when launching my application:

System.Windows.Data Error: ConvertBack cannot convert value 'null' (type 'null'). BindingExpression: Path='Shift1' DataItem='SDB.Zorgplanner.Client.ViewModels.ViewModels.DienstDetailViewModel' (HashCode=13572818); target element is 'SDB.Zorgplanner.Client.Assets.Controls.DienstDetailItemControl' (Name=''); target property is 'DataContext' (type 'System.Object').. System.MethodAccessException: Attempt by method 'System.Windows.CLRPropertyListener.set_Value(System.Object)' to access method 'SDB.Zorgplanner.Client.ViewModels.ViewModels.DienstDetailViewModel.set_Shift1(SDB.Zorgplanner.UIModel.Dienst)' failed.
   bij System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, RuntimeMethodHandleInternal method, RuntimeType parent, UInt32 invocationFlags)
   bij System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, IRuntimeMethodInfo method, RuntimeType parent, UInt32 invocationFlags)
   bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   bij System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   bij System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
   bij System.Windows.CLRPropertyListener.set_Value(Object value)
   bij System.Windows.PropertyAccessPathStep.set_Value(Object value)
   bij System.Windows.Data.BindingExpression.UpdateValue().

Any ideas of how to fix this?

Is there maybe a way to set the datacontext of the tabitems programatically from the viewmodel?

Upvotes: 0

Views: 524

Answers (2)

frG
frG

Reputation: 23

The problem turned out to be a private setter for the Shift1 property... i removed the private attribute and all works fine :)

Upvotes: 1

Boluc Papuccuoglu
Boluc Papuccuoglu

Reputation: 2346

If you look at the error, you will notice that it is thrown by the ConvertBack method. Which is unusual for a DataContext binding, which led me to notice you have a TwoWay mode binding for the DataContext. Unless you have an explicit need for a TwoWay binding, I suggest you switch to a OneWay (or just omit the mode attribute in the XAML, which does the same thing).

Upvotes: 0

Related Questions