1
votes

I am very new on WP programing, I worked with Borland Delphi / PHP / MySQL before..

I want to have combobox or listbox (i know there is no combobox for WP8 any more :( )

I try many options, but my listbox doesn't appear in screen. buttons, text's are all okay by list box never show up at runtime. and also don't get any error on compiler.

Could you help me step by step how to create a combobox / list box for following items:

C,C#,D,D#,E,F,F#,G,G#,A,A#,B

I want to have these in a list, and when user selects one of it i will make some Musical Scale options starting with the key he choose for user.

if you could explain me for WP8:

  1. how to define list box in MainPage.xaml
  2. how to create binding
  3. how to define list in MainPage.xaml.cs and bind to listbox

I install toolkit and try listpicker also, but didn't work eihter.

I add the parts i tried: Hi, in MainPage.xaml i write this code as last try:

        <ListBox ItemsSource="{Binding ScaleSharpx}" Width="50" Background="Blue">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding text}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </StackPanel>

and in MainPage.xaml.cs i tried these

public String[] ScaleSharpx = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

nothing appears on page

if i try this:

<ListBox x:Name="D1" ItemsSource="{Binding}" Width="50" Background="Blue">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding text}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

and then add this code to MainPage.xaml.cs

this.D1.ItemsSource = ScaleSharpx;

i got error message that,

{System.InvalidOperationException: Items collection must be empty before using ItemsSource. at System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value) at Scale_Finder.MainPage..ctor()} System.Exception {System.InvalidOperationException}

1
there are many articles on net explaining those points already. If you have tried something or followed a tutorial but getting non expected result, better to post relevant codes you have tried and explain how it doesn't work. Then people around here can possibly spot what was wrong in your code and how to fix it, instead of rewriting the code from beginning plus explaining it (redundant and could end up the same as tutorial you followed : somehow didn't work in your end).har07
I am not sure how binding works, i tried above examples, and few more, but got nothing on my screen. and last try i got the error message "Items collection must be empty before using ItemsSource" which i couldn't solve yet.GurhanCagin
about that error, you have to choose, either to use binding : ItemsSource={Binding} or assign ItemsSource from code : D1.ItemsSource = ScaleSharpx;. Don't do both at a same time and don't add items manually to listbox : D1.Items.Add(...). Try to fix this one first, then see what problem remains.har07
Thanks for comment, i remove the ItemsSource={Binding} from the last list box, where name D1, and leave the assignment as D1.ItemsSource = ScaleSharpx; but i got same error, "Items collection must be empty before using ItemsSource." :(GurhanCagin
make sure D1.Items doesn't contain any items, as I said, never add item directly to D1.Items or declare listboxitem in XAML.har07

1 Answers

0
votes

That exception means your ListBox already has items in it's Items property. You can't set ItemsSource except if Items property is still empty.

And about your comment "why items are editable?" That's because you use TextBox to display it. If you want items to be displayed as read-only, it is more suitable to use TextBlock instead :

<ListBox x:Name="D1" Width="50" Background="Blue">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>