如何在Listbox中拉伸ListboxItems [英] How to stretch ListboxItems in Listbox

查看:80
本文介绍了如何在Listbox中拉伸ListboxItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个LISTBOX,我动态添加项目。



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



正如你所看到的那样,它不会将物品拉伸到盒子的末端。我希望这个项目能够伸展,从而改变大小。

我怎样才能实现它?



这是我的XAML:

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:

<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"/>





这就是我用C#添加项目的方式:



And this is how I add the item with C#:

CorrWord.Items.Add(String.Format("{0}\t\t{1}", word.ToUpper(), word.Length));







H. orizo​​ntalContentAlignment =拉伸不起作用,我不知道该使用什么。



感谢任何帮助!




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

Appreciate any help!

推荐答案

关键是要修改 ListBox ItemTemplate [ ^ ]像这样:

The key is to modify ListBox ItemTemplate[^] like this:
<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:

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();


这篇关于如何在Listbox中拉伸ListboxItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆