I have a DataTemplateSelector which is applied to a DataGridTemplateColumn. It is correctly providing me with a DataTemplate that varies depending on certain information in my DataRow (in other columns).
So far so good.
However, when I now alter data in my grid which would cause a different DataTemplate to be chosen by the selector for that column it does NOT automatically show this new DataTemplate.
I read in Pro WPF in C# 2008 by Matthew MacDonald (Apress) page 564 that this is known issue and the only way around is to release the Selector and reapply this which would be very slow when there are many records in my table.
Has anyone found a way around this or maybe in .NET4 there is a new feature that combats this issue?
Thanks
Marcel
One solution is to put a ContentPresenter inside the cell. That way when the content changes the ContentPresenter will request the template from the selector again. eg:
<Window
x:Class="TestSAS.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:local="clr-namespace:TestSAS">
<Window.Resources>
<local:MySelector x:Key="mySelector">
<local:MySelector.UpperTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="upper - "></TextBlock>
<TextBlock Text="{Binding}"></TextBlock>
</StackPanel>
</DataTemplate>
</local:MySelector.UpperTemplate>
<local:MySelector.LowerTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="lower - "></TextBlock>
<TextBlock Text="{Binding}"></TextBlock>
</StackPanel>
</DataTemplate>
</local:MySelector.LowerTemplate>
</local:MySelector>
</Window.Resources>
<DockPanel>
<Button DockPanel.Dock="Bottom" Click="doit_Click">Do It</Button>
<DataGrid Name="mainGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding FirstName}" ContentTemplateSelector="{StaticResource mySelector}"></ContentPresenter>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DockPanel>
</Window>
and code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
namespace TestSAS
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
mainGrid.ItemsSource = "Bob,mary,frank,George".Split(',').Select(s => new Person() { FirstName = s }).ToArray();
}
private void doit_Click(object sender, RoutedEventArgs e)
{
((Person[])mainGrid.ItemsSource)[2].FirstName = "Frank";
}
}
public class MySelector : DataTemplateSelector
{
public DataTemplate UpperTemplate { get; set; }
public DataTemplate LowerTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var st = item as string;
if (st == null) return null;
if (st.Substring(0, 1).ToString().ToLower() == st.Substring(0, 1).ToString()) return LowerTemplate;
return UpperTemplate;
}
}
public class Person : INotifyPropertyChanged
{
private string firstName;
public string FirstName
{
get { return firstName; }
set
{
firstName = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
Edit: I have removed my previous answer which was to use a converter instead of a selector. That did work but I think this is a better answer.