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 8008521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:09:49+00:00 2026-06-04T18:09:49+00:00

Ok I have a container that I have created two data templates for. Basically

  • 0

Ok I have a container that I have created two data templates for. Basically one template will show 5 textboxes with and objects data bound to them and the other template will show a button to add that particular object. I subclassed DataTemplateSelector and it works, but when I navigate through my records the Selector never gets called again.

So how would I for the container to reselect it’s template. The container is a StackPanel and I have already tried UpdateVisuals, InvalidateVisuals, InvalidateArrange, and ApplyTemplate.

XAML Code

<DataTemplate x:Key="advisorTemplate">
        <StackPanel Orientation="Vertical" Margin="2,2,2,2" HorizontalAlignment="Stretch" VerticalAlignment="Top">
            <StackPanel Orientation="Horizontal" Margin="2,2,2,2" HorizontalAlignment="Center">
                <extToolkit:WatermarkTextBox Name="txtAcadAdv" Watermark="Acad Adv" Width="125" Margin="2" Text="{Binding Path=Adv.AcadAdv}"/>
                <extToolkit:WatermarkTextBox Name="txtProgAdv" Watermark="Prog Adv" Width="125" Margin="2" Text="{Binding Path=Adv.ProgAdv}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="2,2,2,2" HorizontalAlignment="Center">
                <extToolkit:WatermarkTextBox Name="txtPortAdv" Watermark="Port Adv" Width="125" Margin="2" Text="{Binding Path=Adv.PortAdv}"/>
                <extToolkit:WatermarkTextBox Name="txtEleTws" Watermark="Ele Tws" Width="125" Margin="2" Text="{Binding Path=Adv.EleTws}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="2,2,2,2" HorizontalAlignment="Center">
                <extToolkit:WatermarkTextBox Name="txtMatTws" Watermark="Mat Tws" Width="125" Margin="2" Text="{Binding Path=Adv.MatTws}"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="addAdvisor">
        <Button HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2" Name="btnAddAdvisor" Click="ButtonClick" Content="Add Advisor"/>
    </DataTemplate> 

Initialization of the Content Changed on the Group Box

grpAdv.ContentTemplateSelector = _advisorSelector;

And Finally the Selector Code

private readonly StudentWin _win;

    public  AdvisorDataTemplateSelector(StudentWin win)
    {
        _win = win;
    }

    public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
    {
        var sp = item as StackPanel;
        var adv = sp.DataContext as Advisor;


        if (adv == null)
            return _win.FindResource("addAdvisor") as DataTemplate;

        return _win.FindResource("advisorTemplate") as DataTemplate;
    }

And Here is a snippet of my navigation code

case "btnNext":
                    {
                        if(_view.CurrentPosition < _view.Count - 1)
                        {
                            CheckForUnusedReferences(_view.GetItemAt(_view.CurrentPosition) as Student);
                            _view.MoveCurrentToNext();
                            CheckForNullReferences(_view.CurrentPosition);
                            grpAdv.ApplyTemplate();
                        }
                    }

The two additional Methods are to check if a relationship is null on the student and they will create it and add it to the data context for me or else Entity Framework won’t save the changes. The data templates above will basically help me with a problem of not having the studentId when I try to create a new student.

  • 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-04T18:09:51+00:00Added an answer on June 4, 2026 at 6:09 pm

    I’ve used the following method to force re-application of a DataTemplateSelector.

    Derive from ObservableCollection and add a method that raises NotifyCollectionChangedEventArgs with NotifyCollectionChangedAction.Reset.

    public class MyThingCollection : ObservableCollection<MyThing>
    {
        public void RaiseResetCollection()
        {
            OnCollectionChanged(new 
                NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
    }
    

    Your view model exposes an instance of this type and your ItemsControl binds to that.

    public class MyViewModel : ... (view model base)
    {
        public MyThingCollection Items{get; private set;}
    }
    
    <ItemsControl
         ItemsSource="{Binding Items}"
         ItemsTemplateSelector="{StaticResource MyTemplateSelector}"
         ...
    

    When you need your DataTemplateSelector to be re-applied call RaiseResetCollection on the collection.

    I generally use DataTemplateSelector like this

    public class MyTemplateSelector : DataTemplateSelector
    {
        public DataTemplate Template1 { get; set; }
        public DataTemplate Template2 { get; set; }
    
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            ... return Template1 or Template2 depending on item
        }
        ...
    }
    
    <DataTemplate x:Key="MyTemplate1" DataType="{x:Type MyType1}">
        ...
    </DateTemplate>
    
    <DataTemplate x:Key="MyTemplate2" DataType="{x:Type MyType2}">
        ...
    </DateTemplate>
    
    <local:MyTemplateSelector 
        x:Key="MyTemplateSelector" 
        Template1="{StaticResource MyTemplate1}"
        Template2="{StaticResource MyTemplate2}"
    />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIView container that has two UIImageView s inside it, one partially
I have two tables, one table is the items container, which holds all data
I have created a macro for excel which will pop up form that contains
I have a container element that you can drag objects around in. I want
I have a database that is a container for data that is exported nightly
I have a method that transfers data from one database to another and in
Greetings all, I have a wxPython project (created with wxFormBuilder) that contains two panels,
I have created two JSP programs as follows. First One: Addmulti.jsp <html> <head><title>Upload Excel
In my project I currently have two databases, one that is named ASPNETDB.MDF and
I have a table that contains id, created, user_id. I'm wondering if there is

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.