2012-09-10 74 views
0

我對.NET 4.0 DataGrid有一個很令人不安的問題。我有包含啓用了textWrapping的TextBlock的比例模板列。調整包含包裝TextBlocks的DataGrid的問題調整大小

問題是,在加載時DataGrid的高度不正確(它的大小就好像文本塊全部被包裝在最大值一樣),並且在調整大小時不更新它們的大小。它似乎是一個佈局問題(MeasureOverride和ArrangeOverride似乎在比例尺寸未解決時調用,之後不會調用...),但我無法解決它。

下面是一個簡化的代碼,示出了該問題:

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="700" Width="525"> 
<StackPanel Width="500"> 
    <Button Content="Add DataGrid" Click="Button_Click" /> 
    <ItemsControl x:Name="itemsControl"> 
     <ItemsControl.ItemContainerStyle> 
      <Style> 
       <Setter Property="Control.Margin" Value="5" /> 
      </Style> 
     </ItemsControl.ItemContainerStyle> 
    </ItemsControl> 
</StackPanel> 

MainWindow.xaml.cs

using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Media; 

namespace WpfApplication1 
{ 
public partial class MainWindow : System.Windows.Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     itemsControl.Items.Add(CreateDataGrid()); 
    } 

    private DataGrid CreateDataGrid() 
    { 
     var dataGrid = new DataGrid() { HeadersVisibility = DataGridHeadersVisibility.Column }; 
     dataGrid.MaxWidth = 500; 
     dataGrid.Background = Brushes.LightSteelBlue; 
     dataGrid.Columns.Add(GetDataGridTemplateColumn("Label", "Auto")); 
     dataGrid.Columns.Add(GetDataGridTemplateColumn("Value", "*")); 
     dataGrid.Items.Add(new Entry() { Label = "Text Example 1", Value = "Some wrapped text" }); 
     dataGrid.Items.Add(new Entry() { Label = "Text Example 2", Value = "Some wrapped text" }); 
     return dataGrid; 
    } 

    private DataGridTemplateColumn GetDataGridTemplateColumn(string bindingPath, string columnWidth) 
    { 
     DataGridTemplateColumn result = new DataGridTemplateColumn() { Width = (DataGridLength)(converter.ConvertFrom(columnWidth))}; 
     FrameworkElementFactory cellTemplateframeworkElementFactory = new FrameworkElementFactory(typeof(TextBox)); 
     cellTemplateframeworkElementFactory.SetValue(TextBox.NameProperty, "cellContentControl"); 
     cellTemplateframeworkElementFactory.SetValue(TextBox.TextProperty, new Binding(bindingPath)); 
     cellTemplateframeworkElementFactory.SetValue(TextBox.TextWrappingProperty, TextWrapping.Wrap); 
     result.CellTemplate = new DataTemplate() { VisualTree = cellTemplateframeworkElementFactory }; 
     return result; 
    } 

    private static DataGridLengthConverter converter = new DataGridLengthConverter(); 
} 

public class Entry 
{ 
    public string Label { get; set; } 
    public string Value { get; set; } 
} 

}

enter image description here

enter image description here

回答

0

終於找到了解決方案:在DataGrid上設置CanContentScroll爲false修復了這個問題。

<Setter Property="ScrollViewer.CanContentScroll" Value="False" /> 
0

我終於想出了一個相當骯髒的解決方案,但至少它的工作原理:我手動計算自己的正確值更新網格的高度,考慮到行高,網格填充,網格邊框和ColumnHeaderRow高度。我需要在OnPropertyChanged(用於填充和網格邊框),DataGridColumn.SizeChanged,DataGridColumn.RowUnloaded,ColumnHeaderPresenter.SizeChanged和其他幾個上更新它。

唯一的問題是它與默認DataGrid ControlTemplate正常工作,但如果模板要更改網格渲染,則不再正確。