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 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應