0

我有一个列表框,其数据模板包含一个网格。在网格内我有一个 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 个 ListBoxes 中,我使用相同的数据模板;在 ContentControl 中,WordWrapping 按预期工作(每当我调整窗口大小时),而在 ListBoxes 中则没有。有什么不同?

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

谢谢,亚历山德罗

4

1 回答 1

2

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

<ListBox ... ScrollViewer.HorizontalScrollBarVisibility="Disabled"/>
于 2012-05-16T18:10:08.630 回答