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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:25:52+00:00 2026-05-28T16:25:52+00:00

I have created a DataTemplateSelector class. I would like to apply it to a

  • 0

I have created a DataTemplateSelector class. I would like to apply it to a single item, so it chooses a template based on some conditions.

Essentially I’d like to have the equivalent of this in xaml:

% if myitem.A
  <TextBlock Text="{Binding myitem.data }"/>
% else if myitem.B
  <Button Content="{Binding myitem.data }"/>

 public class MyTemplateSelector : DataTemplateSelector
    {

        public DataTemplate A {
            get;
            set;
        }

        public DataTemplate B {
            get;
            set;
        }


        public override DataTemplate SelectTemplate(object item, DependencyObject container) {
            var myitem = item as MyItem;
            if (myitem.A)
               return A;
            else if (myitem.B)
               return B;
            return base.SelectTemplate(item, container);
        }

    }

How can I achieve this?

Thanks.

  • 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-05-28T16:25:53+00:00Added an answer on May 28, 2026 at 4:25 pm

    If you have one item, not a list of items, you can use ContentPresenter with a custom ContentTemplate to show this item. ContentTemplate can use your TemplateSelector as you know:

           <ContentPresenter x:Name="control">
                <ContentPresenter.ContentTemplate>
                    <DataTemplate>
                        <local:MyTemplateSelector Content="{Binding}">
                            <local:MyTemplateSelector.A>
                                <DataTemplate>
                                    <TextBlock Text="null" />
                                </DataTemplate>
                            </local:MyTemplateSelector.A>
                            <local:MyTemplateSelector.B>
                                <DataTemplate>
                                    <TextBlock Text="{Binding}" />
                                </DataTemplate>
                            </local:MyTemplateSelector.B>
                        </local:MyTemplateSelector>
                    </DataTemplate>
                </ContentPresenter.ContentTemplate>
            </ContentPresenter>
    

    This is my TemplateSelector:

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item == null)
                return A;
    
            return B;
        }
    

    And this line add some content to display:

    control.Content = "123";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a template for Visual Studio 2008 and it currently shows up
I have created a C# class file by using a XSD-file as an input.
i have created a workflow activity that do give the item creater of a
I have created a webservice in .net 2.0, C#. I need to log some
I have created a bar to appear over the keyboard for next/previous/done like the
I have created some forms using zendframework on my local machine that send the
Have created a custom navigation menu in wordpress that has some pages and some
I have created a bucket on Amazon S3 and I kept some images in
I have created a user defined type to contain some data that I will
I have created a LINQ to SQL datacontext with a single datatable in it.

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.