2012-11-23 70 views
-1

不知何故,textwrap無法正常工作。WPF Textwrapping does not work in items control

我是新來的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> 

回答

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


    } 
} 
+0

找到解決方案。我定義了一個isEnabled = false的樣式,其中我設置了高度。當高度自動,它的作品。 無論如何,謝謝 –