1

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?

2 Answers 2

2

Add this ItemTemplate:

<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding}" TextWrapping="Wrap"/>
    </DataTemplate>
</ListBox.ItemTemplate>
Sign up to request clarification or add additional context in comments.

5 Comments

You may also need to set the horizontal scroll bar to "Never", as I believe the default for ListBox is "Auto" so it will just scroll rather than wrapping. Might be wrong though.
@Clemens I can only see 1 item in the list now and not all 3.
Works for me, though, with exactly your XAML. Setting any Scrollbar properties isn't necessary, as the Width of the ListBoxItems is set explicitly by the ItemContainerStyle.
@Clemens the item gets moved to the middle for me and therefore doesn't display anymore.
Then there must be something in your XAML or code you haven't shown us.
1

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;
            }
        }     

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.