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

Find a WPF element inside DataTemplate in the code-behind

I use this function a lot in my WPF programs to find children elements: public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) yield return (T)child; foreach … 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

Stop TabControl from recreating its children

By default, the TabControl shares a panel to render it’s content. To do what you want (and many other WPF developers), you need to extend TabControl like so: TabControlEx.cs [TemplatePart(Name = “PART_ItemsHolder”, Type = typeof(Panel))] public class TabControlEx : TabControl { private Panel ItemsHolderPanel = null; public TabControlEx() : base() { // This is necessary … Read more