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 to disable a databound ListBox item based on a property value?

You can use ItemContainerStyle: <ListBox> <ListBox.ItemContainerStyle> <Style TargetType=”{x:Type ListBoxItem}”> <Style.Triggers> <DataTrigger Binding=”{Binding YourPropertyName}” Value=”False”> <Setter Property=”IsEnabled” Value=”False”/> </DataTrigger> </Style.Triggers> </Style> </ListBox.ItemContainerStyle> </ListBox>

How to bind DataTemplate datatype to interface?

You can bind to interfaces by telling wpf explicitly that you are binding to an interface field: (Please note that ViewModelBase is simply a base-class that implements the INotifyPropertyChanged interface) public class Implementation : ViewModelBase, IInterface { private string textField; public string TextField { get { return textField; } set { if (value == textField) … Read more

WPF: TabControl & DataTemplates

First of all, there are two templates involved here: TabControl.ItemTemplate, used to render the TabItem headers TabControl.ContentTemplate, used to render the TabItem contents If you don’t set these properties explicitly then WPF will attempt to resolve them elsewhere. It will walk up the logical tree looking for a resource telling it how to render your … 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

Display a default DataTemplate in a ContentControl when its content is null or empty?

Simple, you have to bind the content property in the style. Styles won’t overwrite a value on a control if there’s a binding present, even if the value evaluates to Null. Try this. <ContentControl> <ContentControl.Style> <Style TargetType=”ContentControl”> <Setter Property=”Content” Value=”{Binding HurfView.EditedPart}” /> <Style.Triggers> <DataTrigger Binding=”{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Content}” Value=”{x:Null}”> <Setter Property=”ContentControl.Template”> <Setter.Value> <ControlTemplate> <Grid HorizontalAlignment=”Stretch” … 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