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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:06:24+00:00 2026-05-23T23:06:24+00:00

here’s the code. All it does is, it creates a new ListBox control inside

  • 0

here’s the code. All it does is, it creates a new ListBox control inside a StackPanel and adds to it a new Item. The problem is described at the bottom of the page.

MyWindow.xaml

<Window.Resources>
    <DataTemplate x:Key="itemsTemplate">
        <Border Width="200" Height="50" >
            <ListBox ItemsSource="{Binding Title}" />
        </Border>
    </DataTemplate>
</Window.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <ItemsControl Grid.Column="1"
                  Grid.Row="0"
                  ItemsSource="{Binding ElementName=mainWindow, Path=DataItems}"
                  ItemTemplate="{StaticResource itemsTemplate}">
        <ItemsControl.Template>
            <ControlTemplate>
                <ScrollViewer HorizontalScrollBarVisibility="Auto">
                    <ItemsPresenter />
                </ScrollViewer>
            </ControlTemplate>
        </ItemsControl.Template>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</Grid>

MyWindow.xaml.cs

    private ObservableCollection<MyDataItem> dataItems;

    public ObservableCollection<MyDataItem> DataItems
    {
        get { return dataItems; }
    }

    public Window1()
    {
        dataItems = new ObservableCollection<MyDataItem>();
        InitializeComponent();
    }

MyDataItem.cs

public class MyDataItem : DependencyObject
{
    public string Title
    {
        get
        {
            return (string)GetValue(TitleProperty);
        }
        set
        {
            SetValue(TitleProperty, value);
        }
    }

    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(string), 
                                    typeof(MyDataItem), new UIPropertyMetadata(""));

    public MyDataItem(string title)
    {
        Title = title;
    }
}

Add a new Listbox with item

    void addNewItem_Click(object sender, RoutedEventArgs e)
    {
        MyDataItem item = new MyDataItem("12");
        dataItems.Add(item);
        e.Handled = true;
    }

after that action, I see that new ListBox control, but all its item’s characters are treated as a separate items as You can see @ the picture. Why ?

the result

[ EDIT ]

solved, I had to change from string to List everywhere in that class:

public class MyDataItem : DependencyObject
{
    public List<string> Title
    {
        get
        {
            return (List<string>)GetValue(TitleProperty);
        }
        set
        {
            SetValue(TitleProperty, value);
        }
    }

    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(string), 
                                    typeof(MyDataItem), 
                                    new UIPropertyMetadata(new List<string>()));

    public MyDataItem(List<string> title)
    {
        Title = title;
    }
}
  • 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-23T23:06:24+00:00Added an answer on May 23, 2026 at 11:06 pm

    Because you are giving data source as string.. it is converting that string to collection (character array) since itemssource property will always be collection..

    if you still want to create a list box in data template with single item (in you case it is Title).. then try this..

    Change you title property to List and while creating your title create a list which contains one string . so item source will be list and since itemssource contains one string it will create only one item.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code. My question has to do with the append function inside
Here's a problem I ran into recently. I have attributes strings of the form
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see
Here's an interesting problem. On a recently installed Server 2008 64bit I opened IE
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Here’s a problem I’ve really been struggling with. I need to merge two sorted
This is beyond both making sense and my control. That being said here is
here a a piece of code that is supposed to loop over and over
Here is my code: PtyView *v = [[PtyView alloc] init]; [v sendData([charlieImputText stringValue])]; in

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.