Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Because there are two different types of items, I think your best bet would be to create a custom ListBox subclass which <strong>adds a new DependencyProperty to allow you to bind and display a second list</strong>. This would also require a new default style to display the second list appropriately in the same ScrollViewer as the normal <code>&lt;ItemsPresenter/&gt;</code>.</p> <p>Here is an example of my custom ListBox to allow this:</p> <pre><code>public class MyListBox : ListBox { public MyListBox() : base() { this.DefaultStyleKey = typeof(MyListBox); } public static readonly DependencyProperty StaticItemsProperty = DependencyProperty.Register( "StaticItems", typeof(IList), typeof(MyListBox), null); public IList StaticItems { get { return (IList)GetValue(StaticItemsProperty); } set { SetValue(StaticItemsProperty, value); } } } </code></pre> <p>You would then have to copy the entire default ListBox style into your themes/generic.xaml resource dictionary and modify it to become the default style for the MyListBox control. The only thing I modified from the default style (aside from the TargetType attribute) was the content of the ScrollViewer which had the original list:</p> <pre><code>&lt;Style TargetType="custom:MyListBox"&gt; &lt;!-- all the same old XAML for the normal ListBox --&gt; &lt;ScrollViewer x:Name="ScrollViewer" Background="{TemplateBinding Background}" BorderBrush="Transparent" BorderThickness="0" Padding="{TemplateBinding Padding}" TabNavigation="{TemplateBinding TabNavigation}"&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;ItemsControl ItemsSource="{TemplateBinding StaticItems}"&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBox Text="{Binding}"/&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;ItemsPresenter/&gt; &lt;/StackPanel&gt; &lt;/ScrollViewer&gt; &lt;!-- rest of the same old ListBox XAML --&gt; &lt;/Style&gt; </code></pre> <p>As you can see I modified the ScrollViewer which normally just contained the ItemsPresenter for the ListBox and replaced it with a StackPanel containing a new ItemsControl bound to the new StaticItems DependencyProperty I added to MyListBox. I modified the DataTemplate for this ItemsControl to show a TextBox. The normal ItemsPresenter with the normal ItemsTemplate would then show up below the static list in the ScrollViewer.</p> <p>This custom ListBox can then be used in place of a normal ListBox to bind to two different lists in your ViewModel, both to your static items and your dynamic items.</p> <pre><code>&lt;custom:MyListBox x:Name="ListBox" ItemsSource="{Binding DynamicItems}" StaticItems="{Binding StaticItems}"/&gt; </code></pre>
 

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