Click here to Skip to main content
15,906,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have this LISTBOX, which I add items dynamically.

http://www.fileden.com/files/2007/2/5/737151/png.jpg

As you can see it won't stretch the item to the end of the box. I want the item to stretch and therefore change in size.
How can I achieve it?

This is my XAML:
XML
<ListBox x:Name="CorrWord" Grid.Row="1" Grid.Column="4" Grid.RowSpan="3" BorderBrush="Black" BorderThickness="4" FontFamily="Agency FB" removed="{x:Null}" HorizontalContentAlignment="Stretch"/>


And this is how I add the item with C#:
C#
CorrWord.Items.Add(String.Format("{0}\t\t{1}", word.ToUpper(), word.Length));



HorizontalContentAlignment="Stretch" does not work and I don't know what to use.

Appreciate any help!
Posted
Updated 16-Feb-13 9:33am
v3
Comments
aliwpf 16-Feb-13 14:12pm    
hi.are you load word from database?or it is static ?
Member 9808042 16-Feb-13 15:08pm    
Hey! It is first taken from a textbox.text, then I pass it in a method which contain the c# code I have in here.

1 solution

The key is to modify ListBox ItemTemplate[^] like this:
XML
<ListBox x:Name="CorrWord" Grid.Row="1" Grid.Column="4" Grid.RowSpan="3" BorderBrush="Black" BorderThickness="4" FontFamily="Agency FB" HorizontalContentAlignment="Stretch">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <DockPanel Background="LightCoral">
                <TextBlock Text="{Binding}"/>
                <TextBlock Text="{Binding Path=Length}" HorizontalAlignment="Right"/>
            </DockPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

My code like this:
C#
CorrWord.ItemsSource = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.".Split();
 
Share this answer
 
Comments
Member 9808042 16-Feb-13 15:23pm    
Hey Matej! Thanks for your reply!

It did not really solve my problem (sorry for the lack in my explanation). It did however teach me something new!

What I really wanted is to make the size of the text bigger when it stretches with the listbox. Right now as you can see in my picture, it is too small and I want it to change the size of the text (I don't know how) as I change the size of my grid which the ListBox is in.

As in this picture http://www.fileden.com/files/2007/2/5/737151/png2.jpg
Matej Hlatky 16-Feb-13 16:06pm    
So basically, you want to change (increase / decrease) the font size dynamically based on the listbox actual width?
Isn't setting the TextBlock FontSize to larger size, e.g. 20 enough?
I tried to experiment with Viewbox, but I had a problem when resizing back to smaller Width.
There would be third option - to try to use ValueConverter.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900