wpf 如何在WPF中以像素为单位获取网格列宽?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14898545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:33:00  来源:igfitidea点击:

How to get a Grid column width in pixels in WPF?

c#wpfwidthpixels

提问by Mixer

Column widths are specified in different ways (Stars, Auto etc) How to get the width in pixels of a specific column?

列宽以不同方式指定(星号、自动等) 如何获取特定列的宽度(以像素为单位)?

GridLength l=tip.basis.ColumnDefinitions[0].Width;

回答by Brian S

You can use the ActualWidthor ActualHeightproperty of elements to get the width/height of elements. This answerdescribes the difference between `'ActualWidth' and 'Width'.

您可以使用元素的ActualWidthorActualHeight属性来获取元素的宽度/高度。 这个答案描述了“ActualWidth”和“Width”之间的区别。

So in the above example it would be:

所以在上面的例子中,它将是:

Double width = tip.basis.ColumnDefinitions[0].ActualWidth;

And also keep in mind that WPF uses Device Independent Pixels, as described in this answer.

还要记住,WPF 使用与设备无关的像素,如本答案所述

回答by ywm

The ActualWidthproperty should provide the width of the column in Pixels

ActualWidth属性应以像素为单位提供列的宽度

MSDN Page

MSDN页面

回答by Haris Hasan

Use the ActualWidthproperty to get the width. It represents

使用ActualWidth属性获取宽度。它代表了

The width of the column in device-independent units (1/96th inch per unit).

以设备无关单位表示的列的宽度(每单位 1/96 英寸)。