Window.Content with DataTemplate
I have a DataTemplate it's DataType is the MyViewModel, if I do something like:
<ContentPresenter Content="{Binding SomeViewModel}"/>
if I set the "SomeViewModel" to be MyViewModel (the VM that defined in the DataTemplate), I can see the DataTemplate render on the view, which is expected.
What I want to do is this:
Window host = new Window()
host.Content = new MyViewModel();
host.Show();
I expect this to show a w开发者_C百科indow with the DataTemplate that associated with the MyViewModel render on it, instead I get a window with a single line, the path to my ViewModel.
what am I doing wrong ?
Probably a resource location issue. Where was the DataTemplate
defined previously? Was it in App.xaml
s ResourceDictionary? Try adding the DataTemplate
there.
<Application ...>
<Application.Resources>
<DataTemplate DataType="{x:Type MyViewModel}">
<!-- View -->
</DataTemplate>
</Application.Resources>
</Application>
In a better-case scenario you would place this in a ResourceDictionary
that is merged with others in App.xaml
.
Edit: tiny working example.
<Application x:Class="DataTemplateTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Startup="Application_Startup">
<Application.Resources>
<DataTemplate DataType="{x:Type sys:Int32}">
<Border Background="Red">
<TextBlock Text="{Binding}" />
</Border>
</DataTemplate>
</Application.Resources>
</Application>
Appropriate code-behind:
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
var window = new Window();
window.Content = 42;
window.Show();
}
}
Edit 2: Since you said this code is in a WPF AddIn
- If the
DataTemplate
is in the Host application, this will not work. Host and AddIn UIs do not talk to one another in that manner as the AddIn is merely anHwndSource
. If the
DataTemplate
is in aResourceDictionary
in the AddIn, you can load it like so:var window = new Window(); window.Resources.MergedDictionaries.Add( new ResourceDictionary { Source = new Uri("pack://application:,,,/AddInAssembly;component/Resources.xaml", UriKind.Relative) }); window.Content = ...; window.Show();
you should add the following information to your question: where is the datatemplate/usercontrol defined (addin.dll)? where is "window" defined (mainproject)?
if your viewmodel and datatemplate are in your addin.dll and your "window" is in your mainproject, you would have to add your datatemplate as a resource to the window at least.
if your viewmodel and datatemplate and your "window" are in your addin.dll and you just call in your main project "window.show" then you have to add the datatemplate as a resource to the "window" in your addin.dll.
i use mef fo building wpf plugin applications and i use the following syntax in my addin.dlls:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourAddinDllName;Component/ResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
btw if you expose just your viewmodel and call it in your mainproject. you have to expose and register your datatemplate too. let me know how your plugins work.
精彩评论