ModelItem 類別

定義

代表編輯模型中的單一項目。Represents a single item in the editing model. 項目可以是從複雜資料結構到色彩或整數等範圍中的任何項目。An item can be anything from a complex data structure down to a color or integer.

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。The ModelItem can be thought of as a thin proxy for an object at which it points. 請先定義簡單 Animal 物件。First define a simple Animal object.

  
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 ModelItemSecondly, create an instance of that Animal and a ModelItem that is a proxy for it. 之後,即可呼叫 GetCurrentValue 以擷取該物件。The object can then be retrieved by calling GetCurrentValue. 下列程式碼也會示範如何使用 ModelItem 所定義的其他屬性。The following code also shows how to use other properties defined by 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集合存取專案的屬性,並變更屬性的值。You can access the item's properties through its Properties collection and make changes to the values of the properties.

ModelItem 是設計工具基礎資料模型周圍的包裝函式。A ModelItem is a wrapper around the underlying data model of the designer. 您可以透過 GetCurrentValue 方法存取基礎模型。You can access the underlying model through the GetCurrentValue method.

注意

設計工具的序列化和復原系統不會反映您對 GetCurrentValue 方法傳回之物件所進行的任何變更。Any changes you make to an object returned from the GetCurrentValue method will not be reflected by the serialization and undo systems of the designer.

建構函式

ModelItem()

建立 ModelItem 類別的新執行個體。Creates a new instance of the ModelItem class.

屬性

Attributes

取得在這個項目上宣告的屬性。Gets the attributes declared on this item.

Content

取得項目的 ContentPropertyAttribute,或 nullGets the ContentPropertyAttribute of the item, or null.

ItemType

取得項目表示的物件型別。Gets the type of object that the item represents.

Name

取得或設定項目的名稱或 ID。Gets or sets the name or ID of the item.

Parent

取得項目,該項目是這個項目的父項。Gets the item that is the parent of this item.

Parents

取得這個項目的所有父項。Gets all parents of this item.

Properties

取得這個項目的公用屬性。Gets the public properties on this item.

Root

取得項目,該項目是這個樹狀架構的根。Gets the item that is the root of this tree.

Source

取得提供這個值的屬性。Gets the property that provided this value.

Sources

取得保存這個值的所有屬性。Gets all the properties that hold this value.

View

取得以圖形表示這個項目的 DependencyObjectGets a DependencyObject that graphically represents this item.

方法

BeginEdit()

開啟設計工具的編輯範圍。Opens an editing scope for the designer. 開啟編輯範圍後,會將所有物件的變更儲存在範圍中,直到交易完成或還原為止。After an editing scope is open, all changes across all objects will be saved into the scope until the transaction is completed or reverted. 編輯範圍可以是巢狀,但必須依序認可。Editing scopes can be nested, but must be committed in order.

BeginEdit(Boolean)

開啟設計工具的編輯範圍。Opens an editing scope for the designer.

BeginEdit(String)

開啟設計工具的編輯範圍。Opens an editing scope for the designer. 開啟編輯範圍後,會將所有物件的變更儲存在範圍中,直到交易完成或還原為止。After an editing scope is open, all changes across all objects will be saved into the scope until the transaction is completed or reverted. 編輯範圍可以是巢狀,但必須依序認可。Editing scopes can be nested, but must be committed in order.

BeginEdit(String, Boolean)

開啟設計工具的編輯範圍。Opens an editing scope for the designer.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetCurrentValue()

傳回 ModelItem 所包裝之基礎模型物件的目前值。Returns the current value of the underlying model object that the ModelItem is wrapping.

GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回包含在這個模型項目中之基礎模型物件的字串表示。Returns a string representation of the underlying model object contained in this model item.

事件

PropertyChanged

實作 INotifyPropertyChangedImplements INotifyPropertyChanged. 使用這個事件接聽模型的變更。Use this event to listen for changes to the model. WPF 的資料繫結功能也會使用這個方法。This is also used by the data binding features of WPF.

擴充方法

Focus(ModelItem)

在指定的設計工具項目上設定鍵盤焦點。Sets the keyboard focus on the specified designer item.

Focus(ModelItem, Int32)

在指定的設計工具項目上設定鍵盤焦點。Sets the keyboard focus on the specified designer item.

GetEditingContext(ModelItem)

擷取指定模型項目的編輯內容。Retrieves the editing context of the specified model item.

GetModelPath(ModelItem)

擷取指定模型項目的路徑。Retrieves the path of the specified model item.

IsParentOf(ModelItem, ModelItem)

傳回值,這個值表示第一個指定設計工具項目是否為第二個指定設計工具項目的父代。Returns a value that indicates whether the first specified designer item is a parent of the second specified designer item.

適用於