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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:05:20+00:00 2026-05-10T20:05:20+00:00

I know how to create a custom user control in WPF but how can

  • 0

I know how to create a custom user control in WPF but how can I make it so that someone can provide an ItemTemplate?

I have a user control that is a mixture of several other WPF controls, one of them being a ListBox. I’d like to let the user of the control specify the content of the list box but I’m not sure how to pass that information through.

EDIT: The accepted answer works with the following correction:

<UserControl x:Class='WpfApplication6.MyControl'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     xmlns:src='clr-namespace:WpfApplication6'>     <ListBox ItemTemplate='{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:MyControl}}, Path=ItemsSource}' /> </UserControl> 
  • 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. 2026-05-10T20:05:21+00:00Added an answer on May 10, 2026 at 8:05 pm

    You will want to add a DependencyProperty to your control. The xaml will look slightly different if you are deriving from UserControl or Control.

    public partial class MyControl : UserControl {     public MyControl()     {         InitializeComponent();     }      public static readonly DependencyProperty ItemTemplateProperty =         DependencyProperty.Register('ItemTemplate', typeof(DataTemplate), typeof(MyControl), new UIPropertyMetadata(null));     public DataTemplate ItemTemplate     {         get { return (DataTemplate) GetValue(ItemTemplateProperty); }         set { SetValue(ItemTemplateProperty, value); }     } } 

    Here is xaml for a UserControl.

    <UserControl x:Class='WpfApplication6.MyControl'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     xmlns:src='clr-namespace:WpfApplication6'>     <ListBox ItemTemplate='{Binding ItemTemplate, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:MyControl}}}' /> </UserControl> 

    Here is xaml for a Control:

    <Style TargetType='{x:Type src:MyControl}'>     <Setter Property='Template'>         <Setter.Value>             <ControlTemplate TargetType='{x:Type src:MyControl}'>                 <Border Background='{TemplateBinding Background}'                         BorderBrush='{TemplateBinding BorderBrush}'                         BorderThickness='{TemplateBinding BorderThickness}'>                      <ListBox ItemTemplate='{TemplateBinding ItemTemplate}' />                 </Border>             </ControlTemplate>         </Setter.Value>     </Setter> </Style> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I know how to create custom encrypted RSA keys, but how can
I am trying to create a custom user control in WPF. I want to
I have a custom ASP.NET user control that is added to a panel on
I have planned to create custom framework in iOS, and provide it other user
i kinda know how to create a Custom Web Part. But now i need
I want to know if i can create a custom google maps application,on which
I know that if I created a custom control, say MyLabel in App_Code ..
I have a somewhat complex custom user control, created in C# .NET, which is
I have created a custom control (based on a panel) in wxPython that provides
I will have to use lightswitch for a project. I know that we can

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.