6

I have set ItemsSource of a ListBox as follows :

<ListBox ItemsSource="{Binding abc}" />

What I want

<ListBox>
    <listBox.ItemsSource>
        ?????????????
    <listBox.ItemsSource>
</ListBox>
Vishal
  • 6,238
  • 10
  • 82
  • 158
  • that do you expect to put in the `?????` ? I don't understand. – Federico Berasategui Mar 05 '14 at 16:04
  • I expect to replace ????? with some kind of binding. – Vishal Mar 05 '14 at 16:06
  • what for? that doesn't make sense. You are already using a Binding syntax in the first example. Why would you want to convert that to a more verbose syntax? – Federico Berasategui Mar 05 '14 at 16:07
  • Because my ListBox is the child of an ItemsControl. And When I hover over ItemsSource it says ItemsControl.ItemsSource, so I want to explicitly declare ItemsSource on my ListBox. – Vishal Mar 05 '14 at 16:10
  • @HighCore for more information http://stackoverflow.com/questions/22150944/which-control-will-be-more-appropriate-to-the-output-shown-below?noredirect=1#comment33665768_22150944 – Vishal Mar 05 '14 at 16:11
  • 3
    @Vishal: I think you mistakenly believe that your first snippet is setting the `ItemsSource` property of your parent `ItemsControl`, because of what intellisense tooltips are saying. In reality, the `ItemsSource` property of a `ListBox` is inherited from a base class: `ListBox` derives from `ItemsControl` and the latter is where you'll find the property definition. That's why your intellisense says what it does, and in fact there is no difference between the two syntaxes you've shown in terms of which property is being set. – Dan Puzey Mar 05 '14 at 16:18
  • 1
    @DanPuzey after changing my code to the code of accepted answer I really knew that there is not any difference between both the syntaxes. – Vishal Mar 05 '14 at 16:21

4 Answers4

17
<Window xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib">

<ListBox>
<ListBox.ItemsSource>
    <x:Array Type="sys:String">
        <sys:String>1st item</sys:String>
        <sys:String>2nd item</sys:String>
    </x:Array>
<ListBox.ItemsSource>
</ListBox>

</Window>
GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
5
<ListBox>
    <listBox.ItemsSource>
        <Binding Path = "abs" />
    <listBox.ItemsSource>
</ListBox>
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
3

Xamarin Example

If you wandered into this page looking for a Xamarin example (the question seems generic to XAML), then you can try -

<Picker x:Name="picker"
    Title="Select a monkey"
    TitleColor="Red">
  <Picker.ItemsSource>
    <x:Array Type="{x:Type x:String}">
       <x:String>Baboon</x:String>
       <x:String>Capuchin Monkey</x:String>
       <x:String>Blue Monkey</x:String>
       <x:String>Squirrel Monkey</x:String>
       <x:String>Golden Lion Tamarin</x:String>
       <x:String>Howler Monkey</x:String>
       <x:String>Japanese Macaque</x:String>
     </x:Array>
  </Picker.ItemsSource>
</Picker>

From -

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/picker/populating-itemssource#populating-a-picker-with-data

This uses Picker as an example, but the ItemsSource syntax is interchangeable based on the outer control, like so -

<ListView>
  <ListView.ItemsSource>
      <x:Array Type="{x:Type x:String}">
        <x:String>mono</x:String>
        <x:String>monodroid</x:String>
        <x:String>monotouch</x:String>
        <x:String>monorail</x:String>
        <x:String>monodevelop</x:String>
        <x:String>monotone</x:String>
        <x:String>monopoly</x:String>
        <x:String>monomodal</x:String>
        <x:String>mononucleosis</x:String>
      </x:Array>
  </ListView.ItemsSource>
</ListView>
WickedW
  • 2,331
  • 4
  • 24
  • 54
1

@HighCore, @DanPazey, and @Vishal:

In fact, the markup binding syntax may prove to be useful and even necessary.

Not to mention multibinding, consider the following.

Suppose you need to bind your ListBox to CollectionViewSource (for sorting or else). Like this:

<Window.Resources>
    <CollectionViewSource x:Key="abc_CVS_Key" Source="{Binding abc}" />
</Window.Resources>

    <ListBox ItemsSource="{Binding Source={StaticResource abc_CVS_Key}}">
    </ListBox>

You may want then, for technical reasons, to limit a scope of the CVS resource to only the ListBox in question.

If you write down ItemsSource binding in an attribute

    <ListBox ItemsSource="{Binding Source={StaticResource abc_CVS_Key}}">
        <ListBox.Resources>
            <CollectionViewSource x:Key="abc_CVS_Key" Source="{Binding abc}" />
        </List.Resources>
    </ListBox>

your code will compile, but runtime your program will not find your abc_CVS_Key resource key, because the resource has been defined later in code. You need to define the resource before you refer to it in ListBox' ItemsSource binding. Like this:

    <ListBox>
        <ListBox.Resources>
            <CollectionViewSource x:Key="abc_CVS_Key" Source="{Binding abc}" />
        </List.Resources>
        <ListBox.ItemsSource>
            <Binding Source="{StaticResource abc_CVS_Key}" />
        </ListBox.ItemsSource>
    </ListBox>

This code compiles and executes OK.

V.V.T
  • 231
  • 1
  • 7