WPF TextWrapping ListBox和ContentControl之间的差异

时间:2012-05-16 16:49:21

标签: wpf listbox textwrapping

我有一个列表框,其datatemplate包含一个网格。在网格内部我有一个TextBlock,但我无法使TextWrapping属性正常工作:我写了一个示例来说明行为:

XAML:

<Window x:Class="MyTest.Test"
    xmlns:my="clr-namespace:MyTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test" >
<Window.Resources>
    <DataTemplate DataType="{x:Type my:Task}">
        <Border BorderThickness="2" BorderBrush="Red">
            <Grid ShowGridLines="True">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock MaxWidth="50" Margin="3" Grid.Column="0" TextWrapping="Wrap" Text="{Binding Path=Description}"></TextBlock>
                <TextBlock Margin="3" Grid.Column="1" TextWrapping="Wrap"  Text="{Binding Path=LongDescription}"></TextBlock>
            </Grid>
        </Border>
    </DataTemplate>
</Window.Resources>
<StackPanel>

    <TextBlock FontWeight="Bold" TextWrapping="Wrap">ContentControl: the textwrapping works as expected (select a row in the first listbox)</TextBlock>
    <ContentControl MinHeight="10" Content="{Binding ElementName=lsTask, Path=SelectedItem}" />

    <TextBlock FontWeight="Bold" TextWrapping="Wrap">ListBox: I can't get the textwrapping work properly</TextBlock>
    <ListBox MinHeight="10" Name="lsTask" ItemsSource="{Binding Path=TaskList}" ></ListBox>

    <TextBlock FontWeight="Bold" TextWrapping="Wrap">ListBox 2 : I tried with HorizontalContentAlignment="Stretch": similar but doesn't wrap</TextBlock>
    <ListBox MinHeight="10" Name="lsTask2" ItemsSource="{Binding Path=TaskList}" HorizontalContentAlignment="Stretch" ></ListBox>



    <TextBlock FontWeight="Bold" TextWrapping="Wrap">ListBox 3: I can wrap only setting Width in the datatemplate</TextBlock>
    <ListBox MinHeight="10" Name="lsTask3" ItemsSource="{Binding Path=TaskList}" HorizontalContentAlignment="Stretch" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderThickness="2" BorderBrush="Green">
                    <Grid ShowGridLines="True">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <TextBlock Margin="3" Grid.Column="0" TextWrapping="Wrap" Text="{Binding Path=Description}"></TextBlock>
                        <TextBlock  Width="200"  Margin="3" Grid.Column="1" TextWrapping="Wrap"  Text="{Binding Path=LongDescription}"></TextBlock>
                    </Grid>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>


    </ListBox>
</StackPanel>

这是我的 的代码隐藏:

namespace MyTest
{

public class Task
{
    public string Description { get; set; }
    public string LongDescription { get; set; }

}

public class MyContext
{
    public List<Task> TaskList { get; set; }

    public MyContext()
    {
        TaskList = new List<Task>();
        TaskList.Add(new Task() { Description = "description1", LongDescription = "long long long long long long long long description1" });
        TaskList.Add(new Task() { Description = "description2", LongDescription = "long long long long long long long long description2" });
        TaskList.Add(new Task() { Description = "description3", LongDescription = "long long long long long long long long description3" });
    }

}


public partial class Test : Window
{

    public Test()
    {   
        DataContext = new MyContext();
        InitializeComponent();

    }
}
}

在ContentControl和前2个ListBox中,我使用相同的datatemplate;在ContentControl中,WordWrapping按预期工作(每当我调整窗口大小时),而在ListBox中它不会。有什么区别?

我发现使TextWrapping在ListBox中工作的唯一方法是修复TextBlock的宽度(或MaxWidth),但这不是所需的行为。

谢谢, 的Alessandro

1 个答案:

答案 0 :(得分:2)

ListBox包含ScrollViewer,因此当内容想要比ListBox更宽时,会自动显示滚动条。解决方案是禁用水平滚动条。

<ListBox ... ScrollViewer.HorizontalScrollBarVisibility="Disabled"/>