ListBox string displays vertically
My code
Add("STRING");
public void Add(string menu)
{
DataContext = menu.ToString();
}
XAML code of Listbox
<ListBox x:Name="menubox" ItemsSource='{Binding}' Margin="0,5,0,0" Height="244" Width="240" Background="Silver" BorderThickness="0" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Foreground="Blue" FontWeight="Bold" Text="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
But it print like S T R I N G vertically.Each letter as one item.开发者_JAVA百科 How can i print it as singe String lie 'STRING"
You are specifying the itemsource for your listbox to be your string, that means each item (character) within your string will be it's own listboxitem. Therefore you string will be vertical as you are seeing. If you want a listbox with one item in it labeled 'String', try the following:
code: List strArray = new List(); //class level variable
public void Add(string menu)
{
strArray.Add(menu);
DataContext = strArray;
}
That should give you 'String' as your listboxitem.
精彩评论