DataGridColumnStyle.GetPreferredSize(Graphics, Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在派生类中被重写时,将获取指定值的宽度和高度。 在用户定位到使用 DataGridTableStyle 的 DataGridColumnStyle 时将使用该宽度和高度。
protected public:
abstract System::Drawing::Size GetPreferredSize(System::Drawing::Graphics ^ g, System::Object ^ value);
protected internal abstract System.Drawing.Size GetPreferredSize (System.Drawing.Graphics g, object value);
abstract member GetPreferredSize : System.Drawing.Graphics * obj -> System.Drawing.Size
Protected Friend MustOverride Function GetPreferredSize (g As Graphics, value As Object) As Size
参数
- value
- Object
一个对象值,该值是需要知道屏幕高度和宽度的值。
返回
一个 Size,它包含单元格的尺寸。
示例
下面的代码示例使用 GetPreferredSize 方法返回值的最佳大小。
public ref class MyGridColumn: public DataGridTextBoxColumn
{
public:
Size GetPrefSize( Graphics^ g, String^ thisString )
{
return this->GetPreferredSize( g, thisString );
}
};
public ref class Form1: public Form
{
protected:
DataGrid^ dataGrid1;
private:
void GetHeight()
{
MyGridColumn^ myGridColumn;
// Get a DataGridColumnStyle of a DataGrid control.
myGridColumn = dynamic_cast<MyGridColumn^>(dataGrid1->TableStyles[ 0 ]->GridColumnStyles[ "CompanyName" ]);
// Create a Graphics object.
Graphics^ g = this->CreateGraphics();
System::Drawing::Size s = myGridColumn->GetPrefSize( g, "A string" );
}
};
public class Form1: Form
{
protected DataGrid dataGrid1;
private void GetHeight(){
MyGridColumn myGridColumn;
// Get a DataGridColumnStyle of a DataGrid control.
myGridColumn = (MyGridColumn) dataGrid1.TableStyles[0].
GridColumnStyles["CompanyName"];
// Create a Graphics object.
Graphics g = this.CreateGraphics();
Size s =myGridColumn.GetPrefSize(g, "A string");
}
}
public class MyGridColumn:DataGridTextBoxColumn{
public Size GetPrefSize(Graphics g, string thisString){
return this.GetPreferredSize(g,thisString);
}
}
Public Class Form1
Inherits Form
Protected dataGrid1 As DataGrid
Private Sub GetHeight()
Dim myGridColumn As MyGridColumn
' Get a DataGridColumnStyle of a DataGrid control.
myGridColumn = CType(dataGrid1.TableStyles(0). _
GridColumnStyles("CompanyName"), myGridColumn)
' Create a Graphics object.
Dim g As Graphics = Me.CreateGraphics()
Dim s As Size = myGridColumn.GetPrefSize(g, "A string")
End Sub
End Class
Public Class MyGridColumn
Inherits DataGridTextBoxColumn
public Function GetPrefSize(g As Graphics , _
thisString As String ) As Size
return me.GetPreferredSize(g,thisString)
End Function
End Class
注解
使用 GetPreferredSize 确定列的大小应调整到的宽度,给定特定的字符串或数字。
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈