I have a class Wizard which creates a wizard with pages defined in the same project. The PageViewModel is separated from PageView. PageViewModel is an ordinary C# class derived from PageViewModelBase abstract class and PageView is a UserControl. In order to define a mapping between PageViewModel and PageView I wrote the following code for every page in my project:
<Window.Resources>
<DataTemplate DataType="{x:Type OurNewPageViewModel}">
<OurNewPageView />
</DataTemplate>
</Window.Resources>
Now I want to add pages to wizard when the users’ code calls my Wizard’s constructor. It means to move Pages View and ViewModel to the user side. For instance, in order to create a wizard with one page user will write the following code: Wizard usersWizard = new Wizard(new usersViewModel(), new userView()); The problem is I don’t know how to provide the mapping between viewModel and View in my constructor. As far as I understand I can use two different approaches to solve this problem. First, to use FrameworkElementFactory but the following code doesn’t work:
//let we have WelcomePageView wpview and WelcomePageViewModel wpviewmodel
FrameworkElementFactory fef = new FrameworkElementFactory(wpview.GetType());
DataTemplate dt = new DataTemplate();
dt.DataType = wpview.GetType();
dt.VisualTree = fef;
base.Resources.Add(wpviewmodel.GetType(), dt);
Second, to use XamlReader. I can create a data template using it but I don’t know how to attach it to resources.
You use the ItemTemplate to specify the visualization of the data objects. If your ItemsControl is bound to a collection object and you do not provide specific display instructions using a DataTemplate, the resulting UI of each item is a string representation of each object in the underlying collection.
A ControlTemplate will generally only contain TemplateBinding expressions, binding back to the properties on the control itself, while a DataTemplate will contain standard Binding expressions, binding to the properties of its DataContext (the business/domain object or view model).
A data template can contain elements that are each bound to a data property along with additional markup that describes layout, color and other appearance. DataTemplate is, basically, used to specify the appearance of data displayed by a control not the appearance of the control itself.
WPF provides various templates for customization of controls. WPF ListView ItemTemplate is in one of these. ItemTemplate of ListView is useful when we have to change the visual presentation of bound data objects.
In your code-behind to create the DataTemplate, there are a couple of errors:
So your code should be something like:
DataTemplate dt = new DataTemplate();
dt.DataType = typeof(PageViewModel);
FrameworkElementFactory fef = new FrameworkElementFactory(typeof(PageView));
dt.VisualTree = fef;
DataTemplateKey dtKey = new DataTemplateKey(typeof(PageViewModel));
this.Resources.Add(dtKey, dt);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With