Reputation: 31
I`m trying to make an aplication for one of my school projects, and its almost over but now at the end I have a problem.
I have a listbox, and I have a string array that loads text in that listBox. Few Strings are long and text get out of the screen. There is no text wrap or something like that? Please tell me how can I make the text go to second line and not overflow out of the screen?
This is my test code, that is similar to my project. It has the same ListBox and two strings in it with long words.
xaml:
<phone:PhoneApplicationPage
x:Class="Test_Listbox.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox Name="ListboxTest"></ListBox>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
And my cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace Test_Listbox
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
ListboxTest.Items.Add(" List box 1 List box 1 List box 1 List box 1 List box 1 List box 1 List box 1 List box 1 List box 1");
ListboxTest.Items.Add(" List box 2 List box 2 List box 2 List box 2 List box 2 List box 2 List box 2 List box 2 List box 2");
}
}
}
Upvotes: 2
Views: 10227
Reputation: 96
I recommend using a template and a binding.
For instance:
<ListBox Name="ListboxTest" ItemsSource={Binding}>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" TextWrapping="Wrap"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and in your code behind:
List<String> ItemsListProperty{ set; get; }
public MainPage()
{
InitializeComponent();
this.DataContext = ItemsListProperty;
}
You'll need to define the ItemsListProperty, but it's better than adding items directly to the ListBox control.
Upvotes: 6