简体   繁体   English

WPF-如何获取DataGrid中每一行的高度?

[英]WPF - How to get height of each row in DataGrid?

I have a DataGrid that has TextWrapping enabled in cells. 我有一个在单元格中启用了TextWrapping的DataGrid。 Meaning that rows don't have same height. 这意味着行的高度不同。 My question is how do I loop through rows and get whats the height of each one? 我的问题是如何遍历行并获取每一行的高度? How the DataGrid looks like: DataGrid的外观: 在此处输入图片说明

Here is the datagrid code: 这是datagrid代码:

    public ObservableCollection<Article> persons = new ObservableCollection<Article>();

for(int i = 0; i < 35; i++)
{
    persons.Add(new Article("Restless legs syndrome and tis correlation with other sleep problems in the general adult population of Japan",
                         "Minori Enomoto, Lan Li, Sayaka Aritake, Yukihiro Nagase, Tatsuhiko Kaji, Hirokuni Tagaya, Masato Matsuura, Yoshitaka Kaneita, Takashi Ohida, Makoto Uchiyama",
                         "Sleep and Biological Rhythms",
                         4));
}

dgMain.ItemsSource = persons;

Here is how I counted it: 这是我的计算方法:

        private void btnTest_Click(object sender, RoutedEventArgs e)
    {
        int k = 0;
        var rows = GetDataGridRows(dgMain);

        foreach (DataGridRow r in rows)
        {
            var rowHeight = r?.ActualHeight;
            k++;
        }

        MessageBox.Show(k.ToString());
    }

And here is the Article class: 这是Article类:

    public class Article
{
    private string _title;
    public string Title
    {
        get { return this._title; }
        set { this._title = value; }
    }

    private string _authors;
    public string Authors
    {
        get { return this._authors; }
        set { this._authors = value; }
    }

    private string _journal;
    public string Journal
    {
        get { return this._journal; }
        set { this._journal = value; }
    }

    private int _year;
    public int Year
    {
        get { return this._year; }
        set { this._year = value; }
    }

    public Article(string Title, string Authors, string Journal, int Year)
    {
        this._title = Title;
        this._authors = Authors;
        this._journal = Journal;
        this._year = Year;
    }
}            

You can do something like this 你可以做这样的事情

var rows = GetDataGridRows(datagrid);

foreach (DataGridRow r in rows)
 {
       var rowHeight = r?.ActualHeight;
 }
  public IEnumerable<DataGridRow> GetDataGridRows(System.Windows.Controls.DataGrid grid)
        {
            var itemsSource = grid.ItemsSource as IEnumerable;
            if (null == itemsSource) yield return null;
            foreach (var item in itemsSource)
            {
                var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
                if (null != row) yield return row;
            }
        }

Xaml XAML

<StackPanel Orientation="Vertical" >
        <Button Click="Button_Click" Height="39" Width="40"></Button>
        <DataGrid x:Name="dgMain" AutoGenerateColumns="True"  HorizontalAlignment="Left" Height="auto" VerticalAlignment="Top" Width="auto" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" >

        </DataGrid>

    </StackPanel>

ss工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM