0

.NET 4.0 DataGrid に関して非常に気がかりな問題があります。textWrapping が有効になっている TextBlock を含むプロポーショナル テンプレート列があります。

問題は、読み込み時に 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; }
}

}

ここに画像の説明を入力

ここに画像の説明を入力

4

2 に答える 2

0

私は最終的にかなり汚い解決策を思いつきましたが、少なくともそれは機能します:行の高さ、グリッドのパディング、グリッドの境界線、およびColumnHeaderRowの高さを考慮して、正しい値を自分で計算することにより、グリッドの高さを手動で更新しています。OnPropertyChanged (パディングとグリッド ボーダー用)、DataGridColumn.SizeChanged、DataGridColumn.RowUnloaded、ColumnHeaderPresenter.SizeChanged などで更新する必要があります。

唯一の問題は、デフォルトの DataGrid ControlTemplate で正しく機能することですが、テンプレートがグリッド レンダリングを変更する場合は正しく機能しなくなることです。

于 2012-09-11T09:49:51.013 に答える
0

最終的に解決策が見つかりました: DataGrid で CanContentScroll を false に設定すると、問題が修正されました。

<Setter Property="ScrollViewer.CanContentScroll" Value="False" />
于 2012-10-04T07:55:34.157 に答える