2010-11-02 153 views
13

我最近一直在試圖讓WPF(C/4.0)DataGrid中的文本包裝工作,並且無論我實現哪種解決方案(都在包裝中使用某種形式的TextBlock)網格的自動高度,並導致網格底部出現過多的空白(爲了可見性而設置爲黃色)。WPF DataGrid AutoSize問題

我的代碼:(註釋代碼是替代解決方案文本換行,但仍導致過度的空間)

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <DataGrid Grid.Row="0" AutoGenerateColumns="False" ColumnWidth="*" Name="dgFamilyHistories" IsReadOnly="True" 
          HorizontalScrollBarVisibility="Disabled" 
         ItemsSource="{Binding Path=Patient.FamilyHistories}" RowDetailsVisibilityMode="Visible" 
         GridLinesVisibility="All"> 
     <DataGrid.Resources> 
      <Style TargetType="{x:Type DataGridRow}"> 
       <Setter Property="Height" Value="Auto"/> 
      </Style> 
      <!--<Style TargetType="{x:Type DataGridCell}"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type DataGridCell}"> 
          <Border Name="border"> 
           <ContentControl Content="{TemplateBinding Content}"> 
            <ContentControl.ContentTemplate> 
             <DataTemplate> 
              <DockPanel> 
               <TextBlock TextWrapping="WrapWithOverflow" TextTrimming="CharacterEllipsis" 
                 Width="Auto" Height="Auto" Text="{Binding Text}"/> 
              </DockPanel> 
             </DataTemplate> 
            </ContentControl.ContentTemplate> 
           </ContentControl> 
          </Border> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style>--> 
     </DataGrid.Resources> 
     <DataGrid.Background> 
      <SolidColorBrush Color="Yellow" /> 
     </DataGrid.Background> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Date" Binding="{Binding DateEntered, StringFormat={}{0:dd/MM/yyyy}}" Width="85"/> 
      <!--<DataGridTextColumn Header="Relation" Binding="{Binding Relation}"/>--> 
      <DataGridTemplateColumn Header="Relation"> 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <TextBlock TextTrimming="CharacterEllipsis" TextWrapping="Wrap" Text="{Binding Path=Relation}"/> 
          </DataTemplate> 
         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 
      <!--<DataGridTextColumn Header="Illness" Binding="{Binding Illness}"/>--> 
      <DataGridTemplateColumn Header="Illness"> 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <TextBlock TextTrimming="CharacterEllipsis" TextWrapping="Wrap" Text="{Binding Path=Illness}"/> 
          </DataTemplate> 
         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 
      <!--<DataGridTextColumn Header="Health" Binding="{Binding Health}"/>--> 
      <DataGridTemplateColumn Header="Health"> 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <TextBlock TextTrimming="CharacterEllipsis" TextWrapping="Wrap" Text="{Binding Path=Health}"/> 
          </DataTemplate> 
         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 
      <DataGridTextColumn Header="Birth Date" Binding="{Binding DateOfBirth, StringFormat={}{0:dd/MM/yyyy}}" Width="85"/> 
      <DataGridTextColumn Header="Death Date" Binding="{Binding DateOfDeath, StringFormat={}{0:dd/MM/yyyy}}" Width="85"/> 
      <!--<DataGridTextColumn Header="Death Cause" Binding="{Binding CauseOfDeath}"/>--> 
      <DataGridTemplateColumn Header="Death Cause"> 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <TextBlock TextTrimming="CharacterEllipsis" TextWrapping="Wrap" Text="{Binding Path=CauseOfDeath}"/> 
          </DataTemplate> 
         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 
      <DataGridTextColumn Header="Age" Binding="{Binding Age}" Width="50"/> 
     </DataGrid.Columns> 
     <DataGrid.RowDetailsTemplate> 
      <DataTemplate> 
       <Label Name="lblDetails" Content="{Binding Path=Comments}" ContentStringFormat="{}Comments: {0}" Margin="15,0,0,0"/> 
       <DataTemplate.Triggers> 
        <DataTrigger Binding="{Binding Path=Comments, Converter={Converters:IsNullStringConverter}}" Value="True"> 
         <Setter TargetName="lblDetails" Property="Visibility" Value="Collapsed"/> 
        </DataTrigger> 
       </DataTemplate.Triggers> 
      </DataTemplate> 
     </DataGrid.RowDetailsTemplate> 
    </DataGrid> 
    <DockPanel Grid.Row="1" Background="Blue"> 

    </DockPanel> 
</Grid> 

回答

0

是的,我以前遇到過,太多,一定是個錯誤。這個問題實際上不是Wrapping本身,而是一旦細胞變小,那麼DataGrid的高度將不會更新,除非它因任何原因(改變Window的大小或其他)而被重新調整大小。我對這個問題沒有很好的解決方法,但是這裏有一些解決方法。

更新

優化的版本,使用的DataGridColumn的替代的TextBlocks。使用附加屬性WrapColumn(默認爲false)知道包裝的列。

Xaml。爲每個包裝列添加本地:MainWindow.WrapColumn =「True」。

<DataGridTemplateColumn Header="Health" 
         local:MainWindow.WrapColumn="True"> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <TextBlock TextTrimming="CharacterEllipsis" 
         TextWrapping="Wrap" 
         Text="{Binding Path=Health}"/> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

添加附加屬性

public partial class MainWindow : Window 
{ 
    private static readonly DependencyProperty WrapColumnProperty = 
      DependencyProperty.RegisterAttached("WrapColumn", 
               typeof(bool), 
               typeof(MainWindow)); 
    public static void SetWrapColumn(DependencyObject element, bool value) 
    { 
     element.SetValue(WrapColumnProperty, value); 
    } 
    public static bool GetWrapColumn(DependencyObject element) 
    { 
     return (bool)element.GetValue(WrapColumnProperty); 
    } 

添加監聽ActualWidth的改變對於已經WrapColumn設置爲true

public MainWindow() 
{ 
    InitializeComponent(); 

    DependencyPropertyDescriptor dependencyPropertyDescriptor = 
     DependencyPropertyDescriptor.FromProperty(DataGridColumn.ActualWidthProperty, typeof(DataGridColumn)); 

    if (dependencyPropertyDescriptor != null) 
    { 
     foreach (DataGridColumn column in c_dataGrid.Columns) 
     { 
      if (GetWrapColumn(column) == true) 
      { 
       dependencyPropertyDescriptor.AddValueChanged(column, DataGridColumn_ActualWidthChanged); 
      } 
     } 
    } 

    void DataGridColumn_ActualWidthChanged(object sender, EventArgs e) 
    { 
     c_dataGrid.Width = c_dataGrid.ActualWidth - 1; 
     EventHandler eventHandler = null; 
     eventHandler = new EventHandler(delegate 
     { 
      c_dataGrid.Width = double.NaN; 
      c_dataGrid.LayoutUpdated -= eventHandler; 
     }); 
     c_dataGrid.LayoutUpdated += eventHandler; 
    } 
    //... 
} 
+0

此方法適用於每一個的DataGridColumn,但汽車大小在使用流程文檔打印時仍然會感到困惑。如果流程文檔觸發調整其高度顯示正確,但所有列均已壓平。 – 2010-11-07 20:58:48

+0

看到你批准了一段時間後被拒絕,所以我覺得有些東西沒有打球。所以列最終寬度最小或效果如何? – 2010-11-07 21:04:46

+0

非常多,basiclly我正在把網格放在一個流動文檔中,然後我打印它。本質上發生的是,只要流文檔超過一頁,其上的第一個數據網就會混亂。 (整個網格是正確的寬度,標題也是如此,但腳趾細胞全部壓扁到左側,我嘗試併發佈一個屏幕截圖 – 2010-11-07 21:18:34

1
Use this way, to expand your datagrid with proper height and width 
    <my:DataGridTemplateColumn Header="{DynamicResource name}" Width="*" 
                  CanUserSort="True" SortMemberPath="Name" 
                  HeaderStyle="{StaticResource StaffDgColoumnHeaderStyle}"> 
           <my:DataGridTemplateColumn.CellTemplate> 
            <DataTemplate> 
             <TextBlock Style="{StaticResource RowTextblockStyle}" >`enter code here` 
               <Hyperlink> 
                <TextBlock Text="{Binding Path=Name}" ToolTip="{Binding Name}" 
                   TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/> 
               </Hyperlink> 
              </TextBlock> 
            </DataTemplate> 
           </my:DataGridTemplateColumn.CellTemplate> 
          </my:DataGridTemplateColumn> 

          <my:DataGridTemplateColumn Header="{DynamicResource sft}" Width="*" 
                  CanUserSort="True" SortMemberPath="ShiftName" 
                  HeaderStyle="{StaticResource StaffDgColoumnHeaderStyle}"> 
           <my:DataGridTemplateColumn.CellTemplate> 
            <DataTemplate> 
             <TextBlock Text="{Binding ShiftName}" ToolTip="{Binding ShiftName}" 
                 Style="{StaticResource RowTextblockStyle}"/> 
            </DataTemplate> 
           </my:DataGridTemplateColumn.CellTemplate> 
          </my:DataGridTemplateColumn> 

See Width ="*" Or Use Width ="Auto" or Width = "20*" as per as your requirement.