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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:17:32+00:00 2026-05-26T02:17:32+00:00

<ListBox x:Name=listBox> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Margin=10 > <TextBlock Text={Binding title}/> <TextBlock Text={Binding Description}/> </StackPanel>

  • 0
<ListBox x:Name="listBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="10" >
                <TextBlock Text="{Binding title}"/>
                <TextBlock Text="{Binding Description}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

What is the correct way to add this with source code?

edited:

tried this:

public static DataTemplate createDataTemplate()
{
    return (DataTemplate)System.Windows.Markup.XamlReader.Load(
        @"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"">
            <TextBlock Text=""{Binding Title}"" />
            <TextBlock Text=""{Binding Description}"" />
            <Image Source=""{Binding Image}"" />
      </DataTemplate>"
      );
}

when i call this:

for (int i=0; i<10; i++) {
                ListBox lb      = new ListBox();
                lb.ItemTemplate = createDataTemplate();
                //...then add to a pivotitem
}

i get this:

The property ‘System.Windows.FrameworkTemplate.Template’ is set more than once. [Line: 3 Position: 32]

why?

  • 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-26T02:17:33+00:00Added an answer on May 26, 2026 at 2:17 am

    You could simply define the shared template in your App.xaml file under the “Resources” element.

    Define it in App.xaml:

    <DataTemplate x:Key="MySharedTemplate">
        <StackPanel Margin="10" >
            <TextBlock Text="{Binding title}"/>
            <TextBlock Text="{Binding Description}"/>
        </StackPanel>
    </DataTemplate>
    

    Access it in code:

    #region FindResource
    /// <summary>Get a template by the type name of the data.</summary>
    /// <typeparam name="T">The template type.</typeparam>
    /// <param name="initial">The source element.</param>
    /// <param name="type">The data type.</param>
    /// <returns>The resource as the type, or null.</returns>
    private static T FindResource<T>(DependencyObject initial, string key) where T : DependencyObject
    {
        DependencyObject current = initial;
    
        if (Application.Current.Resources.Contains(key))
        {
            return (T)Application.Current.Resources[key];
        }
    
        while (current != null)
        {
            if (current is FrameworkElement)
            {
                if ((current as FrameworkElement).Resources.Contains(key))
                {
                    return (T)(current as FrameworkElement).Resources[key];
                }
            }
    
            current = VisualTreeHelper.GetParent(current);
        }
    
        return default(T);
    }
    #endregion FindResource
    

    Use it in your UI:

    DataTemplate newTemplate = null;
    string templateKey = "MySharedTemplate";
    
    try { newTemplate = FindResource<DataTemplate>(this, templateKey); }
    catch { newTemplate = null; }
    
    if (newTemplate != null)
    {
        this.ListBox1.ItemTemplate = newTemplate;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have xaml that lookes like this <ListBox> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text={Binding Name}
Here is the XAML: <ListBox ItemsSource={Binding Documents} BorderBrush={x:Null}> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation=Vertical> <TextBlock Text={Binding
I have a databound and itemtemplated ListBox: <ListBox x:Name=lbLista ScrollViewer.VerticalScrollBarVisibility=Visible> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation=Horizontal>
My ListBoxItems contain multiple TextBoxes like this: <ListBox Name=myList SelectionChanged=myList_SelectionChanged> <ListBox.ItemTemplate> <DataTemplate> <ListBoxItem> <ListBoxItem.Content>
I have this code in a xaml page: <ListBox Height=524 HorizontalAlignment=Left Margin=12,6,0,0 Name=listBox1 VerticalAlignment=Top
can someone give the C# equivalent of this xamle code? <ListBox Width=400 Margin=10 ItemsSource={Binding
<Border CornerRadius=2,2,2,2 BorderThickness=1 BorderBrush=LightSkyBlue Margin=5,2,2,2 x:Name=PersonBorder Visibility=Collapsed> <StackPanel Orientation=Vertical> <ListBox Name=personListBoxTest BorderThickness=0 ScrollViewer.HorizontalScrollBarVisibility=Hidden ScrollViewer.VerticalScrollBarVisibility=Auto
How can I bind AlbumArt to my listbox? My code: <ListBox Name=albumLb Margin=0,0,-12,0 ItemsSource={Binding
I have a listbox where the items contain checkboxes: <ListBox Style={StaticResource CheckBoxListStyle} Name=EditListBox> <ListBox.ItemTemplate>
I have a listbox, and I have the following ItemTemplate for it: <DataTemplate x:Key=ScenarioItemTemplate>

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.