CompositeCollection + CollectionContainer: Bind CollectionContainer.Collection to property of ViewModel that is used as DataTemplates DataType

Due to the issue with data binding on CollectionContainer as described http://social.msdn.microsoft.com/Forums/vstudio/en-US/b15cbd9d-95aa-47c6-8068-7ae9f7dca88a/collectioncontainer-does-not-support-relativesource?forum=wpf I now use the following approach: <ListBox> <ListBox.Resources> <CollectionViewSource x:Key=”DogCollection” Source=”{Binding Dogs}”/> <CollectionViewSource x:Key=”CatCollection” Source=”{Binding Cats}”/> </ListBox.Resources> <ListBox.ItemsSource> <CompositeCollection> <CollectionContainer Collection=”{Binding Source={StaticResource DogCollection}}”/> <CollectionContainer Collection=”{Binding Source={StaticResource CatCollection}}”/> </CompositeCollection> </ListBox.ItemsSource> <!– … –> </ListBox> Edit: The CompositeCollection class does not derive from FrameworkElement and … Read more

Different views / data template based on member variable

I usually use a ContentControl to display the data, and swap out the ContentTemplate in a trigger based on the property that changes. Here’s an example I have posted on my blog that swaps a template based on a bound property <DataTemplate x:Key=”PersonTemplate” DataType=”{x:Type local:ConsumerViewModel}”> <TextBlock Text=”I’m a Person” /> </DataTemplate> <DataTemplate x:Key=”BusinessTemplate” DataType=”{x:Type local:ConsumerViewModel}”> … Read more

Change Data template dynamically

A DataTemplateSelector does not respond to PropertyChange notifications, so it doesn’t get re-evaluated when your properties change. The alternative I use is DataTriggers that changes the Template based on a property. For example, this will draw all TaskModel objects using a ContentControl, and the ContentControl.Template is based on the TaskStatus property of the TaskModel <DataTemplate … Read more

How to get DataTemplate.DataTrigger to check for greater than or less than?

You could create an IValueConverter, which converts an integer to a boolean based on the CutOff. Then use DataTrigger.Value of True (or False, depending on what you are returning). WPF DataTriggers are strictly equality comparers if I remember correctly. So something similar to: public class CutoffConverter : IValueConverter { public object Convert(object value, Type targetType, … Read more

Is there a way to use data-template inheritance in WPF?

The only thing that I have found do to for this kind of thing is this: <DataTemplate x:Key=”BaseClass”> <!– base class template here –> </DataTemplate> <DataTemplate DataType=”{x:Type app:BaseClass}”> <ContentPresenter Content=”{Binding}” ContentTemplate=”{StaticResource BaseClass}”/> </DataTemplate> <DataTemplate DataType=”{x:Type app:DerivedClass}”> <StackPanel> <ContentPresenter Content=”{Binding}” ContentTemplate=”{StaticResource BaseClass}”/> <!– derived class extra template here –> </StackPanel> </DataTemplate> Basically this creates a “common” … Read more

Create DataTemplate in codebehind

Although Archedius’s method works, officially it is deprecated and instead recommended way to programmatically create a template is to load XAML from a string or a memory stream using the Load method of the XamlReader class like this… public DataTemplate Create(Type type) { StringReader stringReader = new StringReader( @”<DataTemplate xmlns=””http://schemas.microsoft.com/winfx/2006/xaml/presentation””> <” + type.Name + @” … Read more

How do I build a DataTemplate in c# code?

Assuming that you’ve already set up the ItemsSource etc for drpCreditCardNumberWpf… //create the data template DataTemplate cardLayout = new DataTemplate(); cardLayout.DataType = typeof(CreditCardPayment); //set up the stack panel FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel)); spFactory.Name = “myComboFactory”; spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); //set up the card holder textblock FrameworkElementFactory cardHolder = new FrameworkElementFactory(typeof(TextBlock)); cardHolder.SetBinding(TextBlock.TextProperty, new Binding(“BillToName”)); cardHolder.SetValue(TextBlock.ToolTipProperty, “Card Holder … Read more

What is a ViewModelLocator and what are its pros/cons compared to DataTemplates?

Intro In MVVM the usual practice is to have the Views find their ViewModels by resolving them from a dependency injection (DI) container. This happens automatically when the container is asked to provide (resolve) an instance of the View class. The container injects the ViewModel into the View by calling a constructor of the View … Read more

tech