ModelItem 類別

定義

代表編輯模式中的單一項目。 項目可以是從複雜資料結構到色彩或整數等範圍中的任何項目。

public ref class ModelItem abstract : System::ComponentModel::INotifyPropertyChanged
public abstract class ModelItem : System.ComponentModel.INotifyPropertyChanged
type ModelItem = class
    interface INotifyPropertyChanged
Public MustInherit Class ModelItem
Implements INotifyPropertyChanged
繼承
ModelItem
衍生
實作

範例

您可以將 ModelItem 視為物件指向的精簡型 Proxy。 請先定義簡單 Animal 物件。

public class Animal  
{  
    // simple property  
    public string Name { get; set; }  
    // complex property   
    public Location Residence { get; set; }   
    // list   
    public List<Animal> CloseRelatives { get; set; }  
    // dictionary  
    public Dictionary<string, object> Features { get; set; }   
}  

public class Location  
{  
    public string StreetAddress { get; set; }  
    public string City { get; set; }  
    public string State { get; set; }   
}  

接著,請建立屬於該 Animal 的執行個體,以及其 Proxy ModelItem。 之後,即可呼叫 GetCurrentValue 以擷取該物件。 下列程式碼也會示範如何使用 ModelItem 所定義的其他屬性。

EditingContext ec = new EditingContext();  
var companion1 = new Animal { Name = "Houdini the parakeet" };  
var companion2 = new Animal { Name = "Groucho the fish" };  
var animal = new Animal   
   {  
      Name = "Sasha the pug",  
      Residence = new Location   
      {  
         StreetAddress = "123 Main Street",  
         City = "AnyTown",  
         State = "Washington"  
      },  
      Features = new Dictionary<string, object> {   
         {"noise", "snort" },  
         {"MeanTimeUntilNaps", TimeSpan.FromMinutes(15) }  
      },  
      CloseRelatives = new List<Animal> { companion1, companion2 }   
   };  
ModelTreeManager mtm = new ModelTreeManager(ec);  mtm.Load(animal);  
ModelItem mi = mtm.Root;  

//Testing other properties of the class  
ModelItem root = mtm.Root;  
Assert.IsTrue(root.GetCurrentValue() == animal, "GetCurrentValue() returns same object");  
Assert.IsTrue(root.ItemType == typeof(Animal),"ItemType describes the item");  
Assert.IsTrue(root.Parent == null,"root parent is null");  
Assert.IsTrue(root.Source == null, "root source is null");  
Assert.IsTrue(((List<Animal>)root.Properties["CloseRelatives"].ComputedValue)[0] == companion1,   
   "ComputedValue of prop == actual object");  
Assert.IsFalse(((List<Animal>)root.Properties["CloseRelatives"].ComputedValue)[0] == companion2,   
   "ComputedValue of prop == actual object");  
Assert.AreEqual(root.Properties["Residence"].  
   Value.  
   Properties["StreetAddress"].  
   Value.GetCurrentValue(), "123 Main Street", "get actual value back out");  
Assert.AreEqual(root, root.Properties["Residence"].Parent, "property points to owner");  
ModelItem location = root.Properties["Residence"].Value;  
Assert.AreEqual(root.Properties["Residence"], location.Source, "sources point to the right place");  

備註

您可以透過其 Properties 集合存取專案的屬性,並變更屬性的值。

ModelItem 是設計工具基礎資料模型周圍的包裝函式。 您可以透過 GetCurrentValue 方法存取基礎模型。

注意

設計工具的序列化和復原系統不會反映您對 GetCurrentValue 方法傳回之物件所進行的任何變更。

建構函式

ModelItem()

建立 ModelItem 類別的新執行個體。

屬性

Attributes

取得在這個項目上宣告的屬性。

Content

取得項目的 ContentPropertyAttribute,或 null

ItemType

取得項目表示的物件型別。

Name

取得或設定項目的名稱或 ID。

Parent

取得項目,該項目是這個項目的父項。

Parents

取得這個項目的所有父項。

Properties

取得這個項目的公用屬性。

Root

取得項目,該項目是這個樹狀架構的根。

Source

取得提供這個值的屬性。

Sources

取得保存這個值的所有屬性。

View

取得以圖形表示這個項目的 DependencyObject

方法

BeginEdit()

開啟設計工具的編輯範圍。 開啟編輯範圍後,會將所有物件的變更儲存在範圍中,直到交易完成或還原為止。 編輯範圍可以是巢狀的,但必須按照順序進行認可。

BeginEdit(Boolean)

開啟設計工具的編輯範圍。

BeginEdit(String)

開啟設計工具的編輯範圍。 開啟編輯範圍後,會將所有物件的變更儲存在範圍中,直到交易完成或還原為止。 編輯範圍可以是巢狀的,但必須按照順序進行認可。

BeginEdit(String, Boolean)

開啟設計工具的編輯範圍。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetCurrentValue()

傳回 ModelItem 所包裝之基礎模型物件的目前值。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回包含在這個模型項目中之基礎模型物件的字串表示。

事件

PropertyChanged

實作 INotifyPropertyChanged。 使用這個事件接聽模型的變更。 WPF 的資料繫結功能也會使用這個方法。

擴充方法

Focus(ModelItem)

在指定的設計工具項目上設定鍵盤焦點。

Focus(ModelItem, Int32)

在指定的設計工具項目上設定鍵盤焦點。

GetEditingContext(ModelItem)

擷取指定模型項目的編輯內容。

GetModelPath(ModelItem)

擷取指定模型項目的路徑。

IsParentOf(ModelItem, ModelItem)

傳回值,這個值表示第一個指定設計工具項目是否為第二個指定設計工具項目的父代。

適用於