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

  • Home
  • SEARCH
  • 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 253065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:43:50+00:00 2026-05-11T21:43:50+00:00

I have a WPF UserControl with a ListBox and ContentPanel. The ListBox is bound

  • 0

I have a WPF UserControl with a ListBox and ContentPanel. The ListBox is bound to a ObservableCollection that has apples and oranges in it.

What is considered the proper way to have it setup so if I select an apple I see an AppleEditor on the right and if I select an orange an OrangeEditor shows up in the content panel?

  • 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-11T21:43:50+00:00Added an answer on May 11, 2026 at 9:43 pm

    I would suggest using DataTemplating to create and apply the different editors. Depending on how different your ‘apples’ and ‘oranges’ are I would recommend using a DataTemplateSelector. Also, if they had something like a Type property you could also use DataTriggers to switch out the editors.

    Lets set up a small sample with apples and oranges. They’ll have some shared properties, and a few different properties as well. And then we can create an ObservableCollection of the base IFruits to use in the UI.

    public partial class Window1 : Window
    {
        public ObservableCollection<IFruit> Fruits { get; set; }
        public Window1()
        {
            InitializeComponent();
    
            Fruits = new ObservableCollection<IFruit>();
            Fruits.Add(new Apple { AppleType = "Granny Smith", HasWorms = false });
            Fruits.Add(new Orange { OrangeType = "Florida Orange", VitaminCContent = 75 });
            Fruits.Add(new Apple { AppleType = "Red Delicious", HasWorms = true });
            Fruits.Add(new Orange { OrangeType = "Navel Orange", VitaminCContent = 130 });
    
            this.DataContext = this;
        }
    }
    
    public interface IFruit
    {
        string Name { get; }
        string Color { get; }
    }
    
    public class Apple : IFruit
    {
        public Apple() { }
        public string AppleType { get; set; }
        public bool HasWorms { get; set; }
        #region IFruit Members
        public string Name { get { return "Apple"; } }
        public string Color { get { return "Red"; } }
        #endregion
    }
    
    public class Orange : IFruit
    {
        public Orange() { }
        public string OrangeType { get; set; }
        public int VitaminCContent { get; set; }
        #region IFruit Members
        public string Name { get { return "Orange"; } }
        public string Color { get { return "Orange"; } }
        #endregion
    }
    

    Next, we can create DataTemplateSelector, that will just check the type of the Fruit and assign the correct DataTemplate.

    public class FruitTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            string templateKey = null;
    
            if (item is Orange)
            {
                templateKey = "OrangeTemplate";
            }
            else if (item is Apple)
            {
                templateKey = "AppleTemplate";
            }
    
            if (templateKey != null)
            {
                return (DataTemplate)((FrameworkElement)container).FindResource(templateKey);
            }
            else
            {
                return base.SelectTemplate(item, container);
            }
        }
    }
    

    Then in the UI, we can create the two templates for Apples and Oranges, and use the selector to determine which gets applied to our content.

    <Window x:Class="FruitSample.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:FruitSample"
        Title="Fruits"
        Height="300"
        Width="300">
    <Window.Resources>
    
        <local:FruitTemplateSelector x:Key="Local_FruitTemplateSelector" />
    
        <DataTemplate x:Key="AppleTemplate">
            <StackPanel Background="{Binding Color}">
                <TextBlock Text="{Binding AppleType}" />
                <TextBlock Text="{Binding HasWorms, StringFormat=Has Worms: {0}}" />
            </StackPanel>
        </DataTemplate>
    
        <DataTemplate x:Key="OrangeTemplate">
            <StackPanel Background="{Binding Color}">
                <TextBlock Text="{Binding OrangeType}" />
                <TextBlock Text="{Binding VitaminCContent, StringFormat=Has {0} % of daily Vitamin C}" />
            </StackPanel>
        </DataTemplate>
    
    </Window.Resources>
    
    <DockPanel>
        <ListBox x:Name="uiFruitList"
                 ItemsSource="{Binding Fruits}"
                 DisplayMemberPath="Name" />
        <ContentControl Content="{Binding Path=SelectedItem, ElementName=uiFruitList}"
                        ContentTemplateSelector="{StaticResource Local_FruitTemplateSelector}"/>
    </DockPanel>
    </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have WPF ListBox which is bound to a ObservableCollection, when the collection changes,
I have a WPF control, that has a list of Investors, and in the
I have a WPF ListBox with a defined DataTemplate. In that template, I have
I have a WinForms usercontrol hosting a WPF custom Listbox in it. After the
I have a WPF UserControl that contains a ComboBox . I need to attach
I have a WPF UserControl that contains a StackPanel that is made visible as
I have a WPF user control that is dervied from UserControl class. MouseLeftButtonDown is
I have a complex WPF UserControl made of other ContentControl templates which contain sets
I have a really simple WPF UserControl: <UserControl x:Class=dr.SitecoreCompare.WPF.ConnectionEntry xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml x:Name=connEntry BorderBrush=Navy BorderThickness=1
In a WPF UserControl, I have to make to call to a WebService. I

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.