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 ListView with GridViewColumn and DataTemplate

You need to define the data template as CellTemplate for your column: <ListView x:Name=”lbDatabases” Height=”138″ Width=”498″ Canvas.Left=”44″ Canvas.Top=”146″ > <ListView.View > <GridView > <GridViewColumn Header=”Databases” Width=”498″> <GridViewColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked=”{Binding IsActive}” Checked=”AnyChange” Unchecked=”AnyChange” Style=”{x:Null}” Content=”{Binding DbName}” Width=”{Binding CheckWidth}” /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView>

How do I dynamically bind and statically add MenuItems?

You can use a CompositeCollection to do this, you can combine different Collections and add static items in the xaml. Example: Xaml: <Window x:Class=”WpfApplication8.MainWindow” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” Title=”MainWindow” Height=”233″ Width=”143″ Name=”UI”> <Window.Resources> <CollectionViewSource Source=”{Binding ElementName=UI, Path=Windows}” x:Key=”YourMenuItems”/> </Window.Resources> <Grid DataContext=”{Binding ElementName=UI}”> <Menu Height=”24″ VerticalAlignment=”Top”> <MenuItem Header=”_View” > <MenuItem Header=”Windows”> <MenuItem.ItemsSource> <CompositeCollection> <CollectionContainer Collection=”{Binding Source={StaticResource YourMenuItems}}” /> … 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

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

Select ListBoxItem if TextBox in ItemTemplate gets focus

You can trigger on the property IsKeyboardFocusWithin in the ItemContainerStyle and set IsSelected to true. <ListBox.ItemContainerStyle> <Style TargetType=”{x:Type ListBoxItem}”> <Style.Triggers> <DataTrigger Binding=”{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource Self}}” Value=”True”> <DataTrigger.EnterActions> <BeginStoryboard> <Storyboard> <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty=”(ListBoxItem.IsSelected)”> <DiscreteBooleanKeyFrame KeyTime=”0″ Value=”True”/> </BooleanAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </DataTrigger.EnterActions> </DataTrigger> </Style.Triggers> </Style> </ListBox.ItemContainerStyle> You could also use a Setter instead of a single frame animation but … Read more