WPF Textwrapping在项目控件中不起作用

时间:2012-11-23 11:21:19

标签: c# wpf

不知何故textwrap不起作用。

我是WPF的新手,所以如果有人知道为什么这不起作用,我会很感激。

这是我的代码:

                <ItemsControl Grid.ColumnSpan="3" Grid.Row="1" Margin="0 0 0 0" ItemsSource="{Binding VRCItemsPaged}">
                    <ItemsControl.Template>
                        <ControlTemplate>
                            <StackPanel Orientation="Vertical">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="310"/>
                                        <ColumnDefinition Width="110"/>
                                        <ColumnDefinition Width="351"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Style="{StaticResource Label}" Grid.Column="0" Text="Description"/>
                                    <TextBlock Style="{StaticResource Label}" Grid.Column="1" Text="Code"/>
                                    <TextBlock Style="{StaticResource Label}" Grid.Column="2" Text="Value"/>
                                </Grid>
                                <StackPanel Orientation="Vertical" IsItemsHost="True"/>
                            </StackPanel>
                        </ControlTemplate>
                    </ItemsControl.Template>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid Height="Auto">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="280"/>
                                    <ColumnDefinition Width="30"></ColumnDefinition>
                                    <ColumnDefinition Width="80"/>
                                    <ColumnDefinition Width="30"></ColumnDefinition>
                                    <ColumnDefinition Width="351"/>
                                </Grid.ColumnDefinitions>
                                <TextBox FontWeight="Normal" TextWrapping="Wrap" IsEnabled="False" Grid.Column="0" Text="{Binding Path=Description}"/>
                                <TextBox FontWeight="Normal" Grid.Column="2" IsEnabled="False" Text="{Binding Path=Code}"/>
                                <TextBox FontWeight="Normal" Grid.Column="4" IsEnabled="False"    Text="{Binding Path=Value}"/>
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

1 个答案:

答案 0 :(得分:0)

对不起,这是另一个问题,而不是答案,但我需要添加一个屏幕截图,这就是为什么它不是评论。

你的问题实际上没有起作用?我刚刚使用ItemsControl XAML创建了一个简单的测试窗口,我看到文本正确包装。

enter image description here

我使用的XAML是

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <ItemsControl Grid.ColumnSpan="3" Grid.Row="1" Margin="0 0 0 0" ItemsSource="{Binding Items}">
        <ItemsControl.Template>
            <ControlTemplate>
                <StackPanel Orientation="Vertical">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="310"/>
                            <ColumnDefinition Width="110"/>
                            <ColumnDefinition Width="351"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Text="Description"/>
                        <TextBlock Grid.Column="1" Text="Code"/>
                        <TextBlock Grid.Column="2" Text="Value"/>
                    </Grid>
                    <StackPanel Orientation="Vertical" IsItemsHost="True"/>
                </StackPanel>
            </ControlTemplate>
        </ItemsControl.Template>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Height="Auto">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="280"/>
                        <ColumnDefinition Width="30"></ColumnDefinition>
                        <ColumnDefinition Width="80"/>
                        <ColumnDefinition Width="30"></ColumnDefinition>
                        <ColumnDefinition Width="351"/>
                    </Grid.ColumnDefinitions>
                    <TextBox FontWeight="Normal" TextWrapping="Wrap" IsEnabled="False" Grid.Column="0" Text="{Binding Path=Description}"/>
                    <TextBox FontWeight="Normal" Grid.Column="2" IsEnabled="False" Text="{Binding Path=Code}"/>
                    <TextBox FontWeight="Normal" Grid.Column="4" IsEnabled="False"    Text="{Binding Path=Value}"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

背后的代码是。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public ObservableCollection<TestObject> Items
        {
            get { return (ObservableCollection<TestObject>)GetValue(ItemsProperty); }
            set { SetValue(ItemsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Items.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.Register("Items", typeof(ObservableCollection<TestObject>), typeof(MainWindow), new PropertyMetadata(null));



        public MainWindow()
        {
            InitializeComponent();
            Items = new ObservableCollection<TestObject>();
            Items.Add(new TestObject() { Description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." });
            Items.Add(new TestObject() { Description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." });
        }
    }

    public class TestObject : DependencyObject
    {


        public string Description
        {
            get { return (string)GetValue(DescriptionProperty); }
            set { SetValue(DescriptionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Description.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DescriptionProperty =
            DependencyProperty.Register("Description", typeof(string), typeof(TestObject), new PropertyMetadata(null));


    }
}