Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Strange problem with dynamically binding items to a new ListBox
    primarykey
    data
    text
    <p>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.</p> <p><strong>MyWindow.xaml</strong></p> <pre><code>&lt;Window.Resources&gt; &lt;DataTemplate x:Key="itemsTemplate"&gt; &lt;Border Width="200" Height="50" &gt; &lt;ListBox ItemsSource="{Binding Title}" /&gt; &lt;/Border&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="100"/&gt; &lt;ColumnDefinition Width="*"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;ItemsControl Grid.Column="1" Grid.Row="0" ItemsSource="{Binding ElementName=mainWindow, Path=DataItems}" ItemTemplate="{StaticResource itemsTemplate}"&gt; &lt;ItemsControl.Template&gt; &lt;ControlTemplate&gt; &lt;ScrollViewer HorizontalScrollBarVisibility="Auto"&gt; &lt;ItemsPresenter /&gt; &lt;/ScrollViewer&gt; &lt;/ControlTemplate&gt; &lt;/ItemsControl.Template&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;StackPanel Orientation="Horizontal" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;/ItemsControl&gt; &lt;/Grid&gt; </code></pre> <p><strong>MyWindow.xaml.cs</strong></p> <pre><code> private ObservableCollection&lt;MyDataItem&gt; dataItems; public ObservableCollection&lt;MyDataItem&gt; DataItems { get { return dataItems; } } public Window1() { dataItems = new ObservableCollection&lt;MyDataItem&gt;(); InitializeComponent(); } </code></pre> <p><strong>MyDataItem.cs</strong> </p> <pre><code>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; } } </code></pre> <p><strong>Add a new Listbox with item</strong></p> <pre><code> void addNewItem_Click(object sender, RoutedEventArgs e) { MyDataItem item = new MyDataItem("12"); dataItems.Add(item); e.Handled = true; } </code></pre> <p>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 ? </p> <p><img src="https://i.stack.imgur.com/q0xjd.png" alt="the result"></p> <blockquote> <p><strong><code>[ EDIT ]</code></strong></p> </blockquote> <p>solved, I had to change from string to List everywhere in that class:</p> <pre><code>public class MyDataItem : DependencyObject { public List&lt;string&gt; Title { get { return (List&lt;string&gt;)GetValue(TitleProperty); } set { SetValue(TitleProperty, value); } } public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(MyDataItem), new UIPropertyMetadata(new List&lt;string&gt;())); public MyDataItem(List&lt;string&gt; title) { Title = title; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload