在我的Silverlight应用程序中,我有一个TabControl,标签控件有以下项目:
<controls:TabItem Header="{Binding Localization.Shift1}" Style="{StaticResource PanelBarTabItemStyle}" IsTabStop="False">
<local:DienstDetailItemControl DataContext="{Binding Shift1, Mode=TwoWay, Converter= {StaticResource DebugConverter}}"/>
</controls:TabItem>
当我的应用程序启动时,Shift1属性仍为null(当用户选择一个选项时,它将被填充)。
启动我的应用程序时出现以下错误:
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().
有关如何解决此问题的任何想法?
是否有可能以某种方式从视图模型中以编程方式设置tabitems的datacontext?
答案 0 :(得分:1)
问题原来是Shift1属性的私有设置器...我删除了私有属性,一切正常:)
答案 1 :(得分:0)
如果查看错误,您会注意到它被ConvertBack方法抛出。这对于DataContext绑定来说是不寻常的,这让我注意到你有一个DataContext的TwoWay模式绑定。除非您明确需要TwoWay绑定,否则我建议您切换到OneWay(或者只是省略XAML中的mode属性,它执行相同的操作)。