亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

C#
躍然一笑 2022-12-24 13:47:40
我有一個在單元格中啟用了 TextWrapping 的 DataGrid。這意味著行沒有相同的高度。我的問題是如何遍歷行并獲取每一行的高度?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;我是這樣計算的:        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());    }這是文章類:    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;    }}            
查看完整描述

1 回答

?
Helenr

TA貢獻1780條經驗 獲得超4個贊

你可以這樣做


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


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

http://img1.sycdn.imooc.com//63a692ab0001992813620769.jpg

查看完整回答
反對 回復 2022-12-24
  • 1 回答
  • 0 關注
  • 400 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號