TextField 類別
輕量型物件,表示可以顯示文字標籤或區域的圖形部分。
繼承階層架構
System.Object
Microsoft.VisualStudio.Modeling.Diagrams.ShapeField
Microsoft.VisualStudio.Modeling.Diagrams.TextField
Microsoft.VisualStudio.Modeling.Diagrams.LabelTextField
命名空間: Microsoft.VisualStudio.Modeling.Diagrams
組件: Microsoft.VisualStudio.Modeling.Sdk.Diagrams.12.0 (在 Microsoft.VisualStudio.Modeling.Sdk.Diagrams.12.0.dll 中)
語法
'宣告
Public Class TextField _
Inherits ShapeField
public class TextField : ShapeField
TextField 類型會公開下列成員。
建構函式
名稱 | 描述 | |
---|---|---|
![]() |
TextField | 初始化 TextField 類別的新執行個體。 |
回頁首
屬性
名稱 | 描述 | |
---|---|---|
![]() |
AnchoringBehavior | 描述欄位內容應如何相對於父代 ShapeElement 自動調整本身的位置及大小。 (繼承自 ShapeField)。 |
![]() |
DefaultAccessibleDescription | 取得或設定預設可存取描述。 (繼承自 ShapeField)。 |
![]() |
DefaultAccessibleName | 取得或設定可存取物件之圖案欄位的預設名稱。 (繼承自 ShapeField)。 |
![]() |
DefaultAccessibleState | 取得可存取物件之圖案欄位的預設狀態。 (繼承自 ShapeField)。 |
![]() |
DefaultAutoSize | 如果為 true,文字欄位的最小報告大小將擴張以容納顯示文字的長度。可自動調整大小的文字欄位仍會採用 AnchoringBehavior 所指定的大小最小值。 |
![]() |
DefaultBackgroundBrushId | 取得或設定描繪圖案欄位背景的預設筆刷的 ID。 (繼承自 ShapeField)。 |
![]() |
DefaultCommitOnEscape | 取得或設定值,指出 Escape 鍵是否認可編輯。如果為 true,Enter 鍵將做為就地編輯的 CRLF。 |
![]() |
DefaultFocusable | 取得圖案欄位,並確認它是否可以接收焦點,或提供圖案欄位接收焦點的能力。 (繼承自 ShapeField)。 |
![]() |
DefaultFontId | 取得或設定預設字型 ID。 |
![]() |
DefaultInactiveSelectedBackgroundBrushId | 取得或設定圖案欄位的預設背景筆刷的 ID。 (繼承自 ShapeField)。 |
![]() |
DefaultInactiveSelectedTextBrushId | 取得或設定非使用中選定文字的預設筆刷 ID。 |
![]() |
DefaultIsHorizontal | 取得或設定文字的預設水平狀態。 |
![]() |
DefaultMultipleLine | 如果為 true,文字會自動換行為一行以上,以符合可用寬度。 |
![]() |
DefaultPenId | 取得或設定圖案欄位所要使用的預設畫筆的 ID。 (繼承自 ShapeField)。 |
![]() |
DefaultReflectParentFocusedState | 取得子圖案欄位,並確認在其父圖案接收焦點時,它是否應該也接收焦點,或設定反映其父圖案焦點狀態的能力。 (繼承自 ShapeField)。 |
![]() |
DefaultReflectParentSelectedState | 取得子圖案欄位,並確認是否應該在選取其父圖案時也選取它,或設定反映其父圖案選取狀態的能力。 (繼承自 ShapeField)。 |
![]() |
DefaultSelectable | 取得圖案欄位,並確認它是否預設可選取,或設定圖案欄位的預設選取狀態。 (繼承自 ShapeField)。 |
![]() |
DefaultSelectedBackgroundBrushId | 取得或設定圖案欄位的預設背景筆刷的 ID。 (繼承自 ShapeField)。 |
![]() |
DefaultSelectedTextBrushId | 取得或設定選定文字的預設筆刷 ID。 |
![]() |
DefaultStringFormat | 取得或設定文字繪製的預設字串格式。 |
![]() |
DefaultText | 取得或設定要繪製的預設文字。 |
![]() |
DefaultTextBrushId | 取得或設定文字的預設筆刷 ID。 |
![]() |
DefaultVisibility | 取得圖案欄位,並確認它是否預設會出現,或設定圖案欄位的預設可見度。 (繼承自 ShapeField)。 |
![]() |
DisplayMember | 取得或設定與顯示文字相關聯的屬性名稱。 |
![]() |
DrawBorder | (不要使用 - 將會變更) |
![]() |
FillBackground | (不要使用 - 將會變更) |
![]() |
Name | 取得圖案欄位的名稱。 (繼承自 ShapeField)。 |
回頁首
方法
回頁首
備註
當您定義圖案時文字裝飾項目,其由 TextField 物件。 以 TextFields ShapeFields 和其他的使用範例,請檢查 Dsl \ GeneratedCode \在您的 DSL 方案的 Shapes.cs。
TextField 是處理圖形內的區域的物件,例如空間指定至標籤。 一個 TextField 執行個體共用相同類別上有多個圖案之間。 TextField 執行個體不需要每一個執行個體分開儲存標籤的文字:相反地, GetDisplayText(ShapeElement) 方法採用圖案做為參數,並可搜尋圖案和其模型項目的目前狀態的文字相依。
文字欄位的外觀是如何決定的
預設 DoPaint() 方法執行這些工作。 這是其程式碼的簡化簡介:
// Simplified version:
public override void DoPaint(DiagramPaintEventArgs e, ShapeElement parentShape)
{
string text = GetDisplayText(shape);
StringFormat format = GetStringFormat(parentShape);
Brush brush = GetTextBrush(e.View, shape);
using (Font font = GetFont(shape))
{
e.Graphics.DrawString(text, font, brush, format);
}
}
// StringFormat determines whether the string is centered etc.
// To customize statically for all instances of this shape field,
// assign to DefaultStringFormat.
// To customize dynamically or per shape, override this:
public virtual StringFormat GetStringFormat(ShapeElement shape)
{ return DefaultStringFormat; }
// Override to customize the displayed string:
public virtual string GetDisplayText(ShapeElement shape)
{ return this.GetValue(shape).ToString(); }
// Brush determines the text color.
// To change the brush for every field, change the shape’s styleset.
// To customize to a brush in the style set, override GetTextBrushId.
// To change the brush to non-standard color, override this.
// Should take account of whether selected.
public virtual Brush GetTextBrush(DiagramClientView view, ShapeElement shape)
{ return shape.StyleSet.GetBrush(this.GetTextBrushId(view, shape)); }
// Brush ID selects a brush from a StyleSet.
// Either return a member of DiagramBrushes
// or add your own brush to the shape or application’s styleset.
// Override this to change dynamically or per instance.
// To change statically, just assign to default values.
public virtual StyleSetResourceId GetTextBrushId(DiagramClientView view, ShapeElement shape)
{ return IsSelected(view, shape) ? (view.Focused ? DefaultSelectedTextBrushId
: DefaultInactiveSelectedTextBrushId ) : DefaultTextBrushId ;
}
// Font determines the shape and size of the text.
// To change the font for every field, change the shape’s styleset.
// To customize to a font in the style set, override GetFontId.
// To change the font to a non-standard font, override this.
public virtual Font GetFont(ShapeElement shape)
{ return shape.StyleSet.GetFont(GetFontId(shape)); }
// Selects a font from a styleset.
// Either return a member of DiagramFonts or
// add your own font to the shape or application’s styleset.
// To change statically for all instances of this field,
// assign to DefaultFontId.
// To change per shape or dynamically, override this.
public virtual StyleSetResourceId GetFontId(ShapeElement parentShape)
{ return DefaultFontId; }
有幾個其他方法對 Get 和 Default 屬性,例如 DefaultMultipleLine/GetMultipleLine()。 您可以將值指派給預設屬性變更圖案欄位的所有執行個體的值。 由一個圖案執行個體要使值變更到另一個則相依於圖案或其模型項目的狀態,請覆寫 Get 方法。
靜態自訂
如果要變更這個圖案欄位的每個執行個體,第一個找到您是否可以在 DSL 定義中的屬性。 例如,您可以設定字型大小和樣式在屬性視窗中。
否則,然後覆寫您的圖案類別的 InitializeShapeFields 方法,並將值指派給文字欄位的適當的 Default... 屬性。
警告
若要覆寫 InitializeShapeFields(),您必須設定圖案類別的 [產生加倍衍生] 屬性設定為在 DSL 定義中的 true 。
在此範例中,圖形會為使用者註解所使用的文字欄位。 我們要使用標準註解字型。 由於它是從樣式集合中的標準字型,可以設定預設字型 ID:
partial class ExampleShape
{ protected override void InitializeShapeFields(IList<ShapeField> shapeFields)
{
// Fields set up according to DSL Definition:
base.InitializeShapeFields(shapeFields);
// Find and update comment field:
TextField commentField = ShapeElement.FindShapeField(shapeFields, "CommentDecorator") as TextField;
// Use the standard font for comments:
commentField.DefaultFontId = DiagramFonts.CommentText;
動態自訂
若要讓變更相依於圖案或其模型項目的狀態,請取得 TextField 自己的子類別並覆寫一或多個 Get... 方法。 您也必須覆寫您的圖案的 InitializeShapeFields 方法,並在您的類別執行個體取代 TextField 的執行個體。
下列範例將字型文字欄位相依於圖案的模型項目的布林網域屬性的狀態。
使用最小的語言範本,若要執行此範例程式碼,請建立新的 DSL 方案。 將布林值網域屬性 AlternateState 加入至 ExampleElement 網域類別。 將圖示 Decorator 加入至 ExampleShape 類別,並將其儲存至點陣圖檔。 按一下 [轉換所有範本]。 將在 DSL 專案的新程式碼檔,然後插入下列程式碼。
若要測試程式碼,請按 F5,此外,在偵錯方案,開啟範例圖表。 圖示的預設狀態應該會出現。 選取圖案並在屬性視窗中,將 [AlternateState] 屬性的值。 項目名稱的字型應該變更。
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;
...
partial class ExampleShape
{
/// <summary>
/// Compose a list of the fields in this shape.
/// Called once for each shape class.
/// </summary>
protected override void InitializeShapeFields(IList<ShapeField> shapeFields)
{
// Fields set up according to DSL Definition:
base.InitializeShapeFields(shapeFields);
// Replace the text field for NameDecorator:
TextField oldField = ShapeElement.FindShapeField(shapeFields, "NameDecorator") as TextField;
shapeFields.Remove(oldField);
// Replace with my text field based on DSL Definition values:
MyTextField newField = new MyTextField(oldField);
shapeFields.Add(newField);
}
}
/// <summary>
/// Dynamic font depends on state of model element.
/// </summary>
public class MyTextField : TextField
{
public MyTextField(TextField prototype)
: base(prototype.Name)
{
DefaultText = prototype.DefaultText;
DefaultFocusable = prototype.DefaultFocusable;
DefaultAutoSize = prototype.DefaultAutoSize;
AnchoringBehavior.MinimumHeightInLines = prototype.AnchoringBehavior.MinimumHeightInLines;
AnchoringBehavior.MinimumWidthInCharacters = prototype.AnchoringBehavior.MinimumWidthInCharacters;
DefaultAccessibleState = prototype.DefaultAccessibleState;
}
public override System.Drawing.Font GetFont(ShapeElement parentShape)
{
// Access the Boolean domain property of the model element:
if ((parentShape.ModelElement as ExampleElement).AlternateState)
return new System.Drawing.Font("Callisto", 14.0f,
System.Drawing.FontStyle.Italic |
System.Drawing.FontStyle.Bold);
else
return base.GetFont(parentShape);
}
}
樣式集合
前一個範例顯示如何變更文字欄位加入至可用的所有字型。 然而,比較好的方法是將它變更為與圖案或與應用程式的一個一組樣式。 若要這麼做,請覆寫 GetFontId 或 GetTextBrushId ()。
或者,請考慮變更樣式設定您的圖案會覆寫 InitializeResources。 這會變更字型和筆刷的影響所有的圖案欄位。
執行緒安全
這個類型的任何 Public static (在 Visual Basic 中為 Shared) 成員都是安全執行緒。不保證任何執行個體成員是安全執行緒。