Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8159623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:58:56+00:00 2026-06-06T17:58:56+00:00

I have a DataTemplateSelector which is applied to a DataGridTemplateColumn. It is correctly providing

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:58:58+00:00Added an answer on June 6, 2026 at 5:58 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have ContentPresenter with DataTemplateSelector: ... public override DataTemplate SelectTemplate(object item, DependencyObject container) {
I have a ListBox that uses DataTemplateSelector to dynamically decide what template to use
I have built a DataTemplateSelector which I use for showing a different pushpin on
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have deployed numerous report parts which reference the same view however one of them
Have 2 tables in Access 2007, both lists of certain tasks to be accomplished.
I have a generic control which displays an editor based on the type property
I have a requirement to generate a report in WPF which is simply a
I have created a DataTemplateSelector class. I would like to apply it to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.