Ben Clarke
Ben Clarke

Reputation: 1061

Wrap text inside Listbox item

Issue


I have a Listbox that I have bound to an ObservableCollection< string>:

Polls = new ObservableCollection<string>();

DataSet LoadAllPolls = _publisher.GetQuizFeatures(Global.gEpisodeWS.Id);

foreach (DataRow item in LoadAllPolls.Tables[0].Rows)
{
    Polls.Add(item["Description"].ToString());
}

Here is the XAML:

<ListBox x:Name="lb_polls" Background="#ececec" BorderBrush="Transparent" SelectedItem="{Binding SelectedPoll}" ItemsSource="{Binding Polls}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Height" Value="110"/>
            <Setter Property="Width" Value="200"/>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#858585" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#858585" />
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#858585"/>
    </ListBox.Resources>
</ListBox>

Now i want the strings that are being added to the ListBox items to wrap. How can I do this?

Upvotes: 1

Views: 4596

Answers (2)

J. Hasan
J. Hasan

Reputation: 512

This is pretty simple, but i don't know why is not working your code, this my xaml

<Grid Width="600" Height="Auto" >
        <ListBox  ItemsSource="{Binding Polls}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Height" Value="110"/>
                    <Setter Property="Width" Value="200"/>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" TextWrapping="Wrap"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

which is bound to an ObservableCollection Polls and is working fine.

       private void LoadPoll()
        {
            for (int i = 1; i <= 3; i++)
            {
                Polls.Add("Poll" + i.ToString());
            }
        }

        private ObservableCollection<string> _Polls = new ObservableCollection<string>();
        public ObservableCollection<string> Polls
        {
            get { return _Polls; }
            set
            {
                _Polls = value;
            }
        }     

Upvotes: 1

Clemens
Clemens

Reputation: 128061

Add this ItemTemplate:

<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding}" TextWrapping="Wrap"/>
    </DataTemplate>
</ListBox.ItemTemplate>

Upvotes: 2

Related Questions