Question

We used to do this at my previous job and it worked perfect there, but for some reason I cannot get it to work now.

I just want to use an ObjectDataProvider to create an instance of the ViewModel class in XAML so I can reference it for binding, and I've got the DataContext set on the Window to the ViewModel, and I've got a xmlns:local with a fully qualified name with the same namespace as all of my ViewModels.

<Window 
x:Class="TimersXP.TimersHost"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:local="clr-namespace:TimersXP.ViewModels"
Name="TimersHostView"
SizeToContent="Height"
Title="TimersXP"
WindowStartupLocation="CenterScreen"
WindowStyle="ToolWindow"
DataContext="TimersHostViewModel">
<Window.Resources>
    <ObjectDataProvider x:Key="TimersHostViewModel" ObjectInstance="{x:Type local:TimersHostViewModel}"/>
</Window.Resources>

Then later my attempt to use the ViewModel:

<Grid.ContextMenu>
        <ContextMenu ItemsSource="{Binding Source={StaticResource TimersHostViewModel}, Path=Skins}" Style="{DynamicResource styleBanner}"/>
</Grid.ContextMenu>

But my problem is that when I set break points in the TimersHostViewModel parameter-less constructor, they are never hit when debugging. So it seems that the ObjectDataProvider isn't doing it's job correctly. Although the window does get created.

I can see that the break points in my App.xaml.cs and MainWindow.xaml.cs are hit, InitializeComponent is also hit, and even my singleton model class is created. But I cannot for the life of me figure out why my TimersHostViewModel class constructor is never called.

One thing I remember is that in my job we used a line like this:

I see that there is no ObjectInstance, but only ObjectType, so when I tried to remove ObjectInstance and set the ObjectType instead, I get the following error: Object reference not set to an instance of an object, and it is highlighting the ObjectType="{x:Type local:TimersHostViewModel}"...What gives?! Why does it work in one instance and not another? I'm very confused about that.

EDIT: Ok I was able to partly answer my own question, but the real question still remains. So I was able to get the ViewModel constructor to be called by removing the DataContext for the Window, and changing the ObjectDataProvider ObjectInstance to ObjectType. Then setting the Grid DataContext to bind to the TimersHostViewModel. But that still leaves me now with this design-time/Compile-time error on the ObjectDataProvider ObjectType: Object reference not set to an instance of an object.

<Window 
x:Class="TimersXP.TimersHost"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:local="clr-namespace:TimersXP.ViewModels"
Name="TimersHostView"
SizeToContent="Height"
Title="TimersXP"
WindowStartupLocation="CenterScreen"
WindowStyle="ToolWindow">
<Window.Resources>
    <ObjectDataProvider x:Key="TimersHostViewModel" ObjectType="{x:Type local:TimersHostViewModel}"/>
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource TimersHostViewModel}}">

Also still not sure why it would work under some circumstances but not others, like the one I'm using now.

Full source code here: http://timersxp.codeplex.com/SourceControl/latest#VS2013/TimersXP/Views/TimersHost.xaml

Was it helpful?

Solution

Had to add the IsAsynchronous="True" property to the ObjectDataProvider, then everything works just peachy! Thanks to this thread for the heads up! http://forums.asp.net/t/1344386.aspx?How+to+create+a+flipcart+like+panel+for+showing+products+in+gridview

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top