DependencyPropertyChangedEventArgs 结构
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供各种属性更改事件的数据。 通常这些事件对只读依赖属性的值中的有效值更改进行报告。 另一个用法是用作 PropertyChangedCallback 实现的一部分。
public value class DependencyPropertyChangedEventArgs
public struct DependencyPropertyChangedEventArgs
type DependencyPropertyChangedEventArgs = struct
Public Structure DependencyPropertyChangedEventArgs
- 继承
示例
下面的示例在 DependencyPropertyChangedEventArgs 的上下文 PropertyChangedCallback 中使用 类,用于自定义类的特定属性,该属性也定义了事件。 回调从属性系统中获取由 传达 DependencyPropertyChangedEventArgs的新旧值的结果,并将这些结果重新打包到不同的事件参数类 RoutedPropertyChangedEventArgs<T>中。 然后,新参数用作自定义类定义和引发的“ValueChanged”事件的数据。
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(
"Value", typeof(decimal), typeof(NumericUpDown),
new FrameworkPropertyMetadata(MinValue, new PropertyChangedCallback(OnValueChanged),
new CoerceValueCallback(CoerceValue)));
private static object CoerceValue(DependencyObject element, object value)
{
decimal newValue = (decimal)value;
newValue = Math.Max(MinValue, Math.Min(MaxValue, newValue));
return newValue;
}
private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
NumericUpDown control = (NumericUpDown)obj;
RoutedPropertyChangedEventArgs<decimal> e = new RoutedPropertyChangedEventArgs<decimal>(
(decimal)args.OldValue, (decimal)args.NewValue, ValueChangedEvent);
control.OnValueChanged(e);
}
/// <summary>
/// Identifies the ValueChanged routed event.
/// </summary>
public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent(
"ValueChanged", RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<decimal>), typeof(NumericUpDown));
/// <summary>
/// Occurs when the Value property changes.
/// </summary>
public event RoutedPropertyChangedEventHandler<decimal> ValueChanged
{
add { AddHandler(ValueChangedEvent, value); }
remove { RemoveHandler(ValueChangedEvent, value); }
}
/// <summary>
/// Raises the ValueChanged event.
/// </summary>
/// <param name="args">Arguments associated with the ValueChanged event.</param>
protected virtual void OnValueChanged(RoutedPropertyChangedEventArgs<decimal> args)
{
RaiseEvent(args);
}
Public Shared ReadOnly ValueProperty As DependencyProperty = DependencyProperty.Register("Value", GetType(Decimal), GetType(NumericUpDown), New FrameworkPropertyMetadata(MinValue, New PropertyChangedCallback(AddressOf OnValueChanged), New CoerceValueCallback(AddressOf CoerceValue)))
Private Shared Overloads Function CoerceValue(ByVal element As DependencyObject, ByVal value As Object) As Object
Dim newValue As Decimal = CDec(value)
newValue = Math.Max(MinValue, Math.Min(MaxValue, newValue))
Return newValue
End Function
Private Shared Sub OnValueChanged(ByVal obj As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
Dim control As NumericUpDown = CType(obj, NumericUpDown)
Dim e As New RoutedPropertyChangedEventArgs(Of Decimal)(CDec(args.OldValue), CDec(args.NewValue), ValueChangedEvent)
control.OnValueChanged(e)
End Sub
''' <summary>
''' Identifies the ValueChanged routed event.
''' </summary>
Public Shared ReadOnly ValueChangedEvent As RoutedEvent = EventManager.RegisterRoutedEvent("ValueChanged", RoutingStrategy.Bubble, GetType(RoutedPropertyChangedEventHandler(Of Decimal)), GetType(NumericUpDown))
''' <summary>
''' Occurs when the Value property changes.
''' </summary>
Public Custom Event ValueChanged As RoutedPropertyChangedEventHandler(Of Decimal)
AddHandler(ByVal value As RoutedPropertyChangedEventHandler(Of Decimal))
MyBase.AddHandler(ValueChangedEvent, value)
End AddHandler
RemoveHandler(ByVal value As RoutedPropertyChangedEventHandler(Of Decimal))
MyBase.RemoveHandler(ValueChangedEvent, value)
End RemoveHandler
RaiseEvent(ByVal sender As System.Object, ByVal e As RoutedPropertyChangedEventArgs(Of Decimal))
End RaiseEvent
End Event
''' <summary>
''' Raises the ValueChanged event.
''' </summary>
''' <param name="args">Arguments associated with the ValueChanged event.</param>
Protected Overridable Sub OnValueChanged(ByVal args As RoutedPropertyChangedEventArgs(Of Decimal))
MyBase.RaiseEvent(args)
End Sub
注解
对事件数据使用 DependencyPropertyChangedEventArgs 类的事件和 DependencyPropertyChangedEventHandler 处理程序的方法实现通常遵循命名模式 Is*Changed
,并且通常作为公共语言运行时 (CLR) 事件实现,而不 RoutedEvent (它们不是路由事件) 。 某些类处理方法“处理”通过属性更改报告状态更改的未公开事件,例如 ButtonBase.OnIsPressedChanged,也对事件数据使用 DependencyPropertyChangedEventArgs 类。
的方案 PropertyChangedCallback 是使用 参数来报告来自属性的属性系统评估中的旧值和新值。 处理新旧值的回调可能会根据这些值选择特殊处理,例如选择不响应被视为微不足道的值更改。
构造函数
DependencyPropertyChangedEventArgs(DependencyProperty, Object, Object) |
初始化 DependencyPropertyChangedEventArgs 类的新实例。 |
属性
NewValue |
获取发生更改之后的属性的值。 |
OldValue |
获取发生更改之前的属性的值。 |
Property |
获取发生值更改的依赖项属性的标识符。 |