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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:57:36+00:00 2026-05-28T01:57:36+00:00

I am trying to create a generic view, and I want it to contain

  • 0

I am trying to create a generic view, and I want it to contain a ListBox with a datatemplate

I want to create it either using pure C# code or if possible load it through xaml? If I can create a template I can get in c# as a resource of sorts.

What I have made until now is

        private static ListBox CreateDayListBox()
    {
        var listBox = new ListBox();
        var dataTemplate = new DataTemplate();

        var grid = new Grid();
        var columnDefinition1 = new ColumnDefinition {Width = GridLength.Auto};
        var columnDefinition2 = new ColumnDefinition();

        grid.ColumnDefinitions.Add(columnDefinition1);
        grid.ColumnDefinitions.Add(columnDefinition2);

        var rectangleItemBought = new Rectangle {Width = 50, Height = 50};
        rectangleItemBought.SetBinding(Rectangle.FillProperty, new Binding("Bought"));
        grid.Children.Add(rectangleItemBought);

        var textBlockItemName = new TextBlock();
        textBlockItemName.SetBinding(TextBlock.TextProperty, new Binding("Name"));
        var textBlockItemQuantity = new TextBlock();
        textBlockItemQuantity.SetBinding(TextBlock.TextProperty, new Binding("Quantity"));
        var textBlockItemQuantityType = new TextBlock();
        textBlockItemQuantityType.SetBinding(TextBlock.TextProperty, new Binding("QuantityType"));

        var stackpanel = new StackPanel();
        Grid.SetColumn(stackpanel, 1);
        stackpanel.Children.Add(textBlockItemName);
        stackpanel.Children.Add(textBlockItemQuantity);
        stackpanel.Children.Add(textBlockItemQuantityType);
        grid.Children.Add(stackpanel);

        return listBox;
    }

So I want the listbox datatemplate to contain 1 rectangle, 1 stackpanel with 3 textboxes inside

  • 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-28T01:57:36+00:00Added an answer on May 28, 2026 at 1:57 am

    You can write the template in XAML then load it in your code.

    Read this.

    Besides, I’m pretty sure you can create the DataTemplate by code the same way you created the controls, look at this code (credit):

    DataTemplate template = new DataTemplate();
    FrameworkElementFactory factory =
      new FrameworkElementFactory(typeof(StackPanel));
    template.VisualTree = factory;
    FrameworkElementFactory childFactory =
      new FrameworkElementFactory(typeof(Image));
    childFactory.SetBinding(Image.SourceProperty, new Binding("Machine.Thumbnail"));
    childFactory.SetValue(Image.WidthProperty, 170.0);    
    childFactory.SetValue(Image.HeightProperty, 170.0);
    factory.AppendChild(childFactory);
    childFactory = new FrameworkElementFactory(typeof(Label));
    childFactory.SetBinding(Label.ContentProperty,
      new Binding("Machine.Descriiption"));
    childFactory.SetValue(Label.WidthProperty, 170.0);
    childFactory.SetValue(Label.HorizontalAlignmentProperty,
      HorizontalAlignment.Center);
    factory.AppendChild(childFactory);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a generic, reusable view, that looks like a lined notepad.
I am trying to create a generic list of objects using reflection. The below
I am trying to create a generic table editor using Entity Framework. I would
Trying to create a generic video plugin for Expression Engine 2 where I can
I am trying to create a generic factory-pattern-like mechanism. The factory will be like:
I am trying to create a generic list box in OpenGl ES (for the
I'm trying to create a generic class in PHP that will provide a way
I am trying to create a generic formatter/parser combination. Example scenario: I have a
I am trying to create a generic class which new's up an instance of
I'm trying to create a generic build template for my Makefiles, kind of like

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.