Informations
Le sujet que vous avez demandé est indiqué ci-dessous. Toutefois, ce sujet ne figure pas dans la bibliothèque.

DependencyObject.SetValue, méthode (DependencyProperty, Object)

Sets the local value of a dependency property, specified by its dependency property identifier.

Espace de noms: System.Windows
Assembly : WindowsBase (dans windowsbase.dll)

public void SetValue (
	DependencyProperty dp,
	Object value
)
public void SetValue (
	DependencyProperty dp, 
	Object value
)
public function SetValue (
	dp : DependencyProperty, 
	value : Object
)
Vous ne pouvez pas utiliser des méthodes dans XAML.

Paramètres

dp

The identifier of the dependency property to set.

value

The new local value.

Type d'exceptionCondition

InvalidOperationException

Attempted to modify a read-only dependency property, or a property on a sealed DependencyObject.

ArgumentException

value was not the correct type as registered for the dp property.

If the provided type does not match the type that is declared for the dependency property as it was originally registered, an exception is raised. The value parameter should always be provided as the appropriate type.

The exception conditions are potentially influenced by the ValidateValueCallback callback that exists on the dependency property identifier of the dependency property being set. Otherwise, the value provided might be failing general type-checking conditions.

This example shows how to back a common language runtime (CLR) property with a DependencyProperty field, thus defining a dependency property. When you define your own properties and want them to support many aspects of Windows Presentation Foundation (WPF) functionality, including styles, data binding, inheritance, animation, and default values, you should implement them as a dependency property.

The following example first registers a dependency property by calling the Register method. The name of the identifier field that you use to store the name and characteristics of the dependency property must be the Name you chose for the dependency property as part of the Register call, appended by the literal string Property. For instance, if you register a dependency property with a Name of Location, then the identifier field that you define for the dependency property must be named LocationProperty.

In this example, the name of the dependency property and its CLR accessor is State; the identifier field is StateProperty; the type of the property is Boolean; and the type that registers the dependency property is MyStateControl.

If you fail to follow this naming pattern, designers might not report your property correctly, and certain aspects of property system style application might not behave as expected.

You can also specify default metadata for a dependency property. This example registers the default value of the State dependency property to be false.

public class MyStateControl : ButtonBase
{
  public MyStateControl() : base() { }
  public Boolean State
  {
    get { return (Boolean)this.GetValue(StateProperty); }
    set { this.SetValue(StateProperty, value); } 
  }
  public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
    "State", typeof(Boolean), typeof(MyStateControl),new PropertyMetadata(false));
}

For the complete sample, see Custom Classes with Dependency Properties Sample.

For more information about how and why to implement a dependency property, as opposed to just backing a CLR property with a private field, see Dependency Properties Overview.

Microsoft .NET Framework 3.0 est pris en charge sur Windows Vista, Microsoft Windows XP SP2 et Windows Server 2003 SP1.

.NET Framework

Prise en charge dans : 3.0

Ajouts de la communauté

Afficher:
© 2014 Microsoft