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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:21:06+00:00 2026-05-27T07:21:06+00:00

Sample Application: The sample application that the supplied code belongs to displays a list

  • 0

Sample Application:

The sample application that the supplied code belongs to displays a list of Vehicle objects via Binding. The Vehicle class is a top level class that subclasses can derive from e.g. Car and Bike. The sample application at the moment displays the owner’s name of the Vehicle.

Sample Model code:

public class Vehicle
{
    private string _ownerName;
    public string ownerName
    {
        get { return _ownerName; }
        set { _ownerName = value; }
    }
}

public class Car : Vehicle
{
    public int doors;
}

public class Bike : Vehicle
{
    // <insert variables unique to a bike, ( I could not think of any...)>
}

UserControl XAML Code:

<Grid>
    <Grid.Resources>
        <DataTemplate x:Key="itemTemplate">
            <WrapPanel>
                <TextBlock Text="{Binding Path=ownerName}"/>
            </WrapPanel>
        </DataTemplate>
    </Grid.Resources>
    <ListBox x:Name="list" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5" ItemsSource="{Binding}" ItemTemplate="{StaticResource itemTemplate}" />
</Grid>

UserControl code behind:

    public List<Vehicle> vehicleList = new List<Vehicle>();

    public CustomControl()
    {
        InitializeComponent();
        createSomeVehicles();
        list.DataContext = vehicleList;
    }

    public void createSomeVehicles()
    {
        Car newcar = new Car();
        newcar.doors = 5;
        newcar.ownerName = "mike";

        Bike newbike = new Bike();
        newbike.ownerName = "dave";

        vehicleList.Add(newcar);
        vehicleList.Add(newbike);
    }

What I want to be able to do:

I would like to be able to display a button in the list object dependant upon the Type of the Vehicle object. E.g. I would like to display a Open Boot button within the list item for Car‘s; Type Bike does not have a boot and so no button would display within the list item.

Idea’s on how to accomplish this:

I have looked into the custom binding of different DataTemplates based upon what type of object it is. E.g. from the code behind I could call:

object.Template = (ControlTemplate)control.Resources["templateForCar"];

The problem here is that I am using a Binding on the whole list and so there is no way to manually bind a DataTemplate to each of the list items, the list binding controls the DataTemplate of it’s items.

  • 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-27T07:21:07+00:00Added an answer on May 27, 2026 at 7:21 am

    You can create a DataTemplate for each Bike and Car (and for any CLR type). By specifying the DataTemplate‘s DataType property, the template will automatically be applied whenever WPF sees that type.

    <Grid>
        <Grid.Resources>
            <DataTemplate DataType="{x:Type local:Car}">
                <WrapPanel>
                    <TextBlock Text="{Binding Path=ownerName}"/>
                    <Button Content="Open Boot" ... />
                </WrapPanel>
            </DataTemplate>
            <DataTemplate DataType="{x:Type local:Bike}">
                <WrapPanel>
                    <TextBlock Text="{Binding Path=ownerName}"/>
                </WrapPanel>
            </DataTemplate>
        </Grid.Resources>
        <ListBox x:Name="list" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5" ItemsSource="{Binding}" />
    </Grid>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running a sample application that has a map component to it. The app
I fired up a sample application that uses Amazon S3 for image hosting. I
Visual Studio 2005 C++ Windows XP Pro I have a sample application that I
Does MS have a sample enterprise application that demonstrates the use of different Enterprise
I am trying to develop a sample application that finds the process name of
I found one sample application from the Blackberry knowledgebase. From that application I have
I have created sample application that insert 12 rows in tableview.And inserted fine.When after
I'm writing a sample application that is both GDI and WPF. I have a
I’m making a sample application that uses MigLayout in a very cool way. Unfortunately
I am new to iphone application development. I have a sample application that needs

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.