/// <summary> /// Translates the HorizontalContentAlignment value into a TextAlignment value. /// </summary> /// <param name="dependencyObject">The Dependency Property that has changed.</param> /// <param name="dependencyPropertyChangedEventArgs">The event arguments describing the property change.</param> public static void OnHorizontalContentAlignmentChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) { // The basic idea here is to translate the HorizontalContentAlignment used by the Control class into a TextAlignment // value that is usable by the TextBlock class. This allows a Control to host a TextBlock with a consistent approach // to how the alignment of the content is specified. ValueBlock valueBlock = (ValueBlock)dependencyObject; HorizontalAlignment horizontalAlignment = (HorizontalAlignment)dependencyPropertyChangedEventArgs.NewValue; // Translate the HorizontalContentAlignment into a TextAlignment that TextBlocks can use. switch (horizontalAlignment) { case HorizontalAlignment.Center: valueBlock.SetValue(ValueBlock.TextAlignmentProperty, TextAlignment.Center); break; case HorizontalAlignment.Left: valueBlock.SetValue(ValueBlock.TextAlignmentProperty, TextAlignment.Left); break; case HorizontalAlignment.Right: valueBlock.SetValue(ValueBlock.TextAlignmentProperty, TextAlignment.Right); break; } }
/// <summary> /// Handles a change to the format of the data. /// </summary> /// <param name="dependencyObject">The dependency object that has been changed.</param> /// <param name="dependencyPropertyChangedEventArgs">The event arguments describing the change to the property.</param> public static void OnFormatChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) { // Reformat the text in the control when the format changes. ValueBlock valueBlock = dependencyObject as ValueBlock; String format = dependencyPropertyChangedEventArgs.NewValue as String; Object value = valueBlock.GetValue(ValueBlock.ContentProperty); valueBlock.SetValue(TextBlock.TextProperty, String.Format(format, value)); }
/// <summary> /// Handles a change to the value of this control. /// </summary> /// <param name="dependencyObject">The dependency object that has been changed.</param> /// <param name="dependencyPropertyChangedEventArgs">The event arguments describing the change to the property.</param> public static void OnContentChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) { // Display the value using the Format property in the Text property of the base class. ValueBlock valueBlock = dependencyObject as ValueBlock; String format = valueBlock.GetValue(ValueBlock.FormatProperty) as String; Object value = dependencyPropertyChangedEventArgs.NewValue; valueBlock.SetValue(ValueBlock.TextProperty, String.Format(format, value)); // Recycling this control can lead to false Up/Down indications because the old value is no longer relavent to the new // value. When the data context of the control has changed, the next property update will not attempt to trigger an // Up/Down event. if (valueBlock.ignoreChange) { valueBlock.ignoreChange = false; } else { // This provides feedback indicating whether the control has increased or decreased in value. if (dependencyPropertyChangedEventArgs.NewValue is IComparable && dependencyPropertyChangedEventArgs.OldValue is IComparable) { IComparable iComparable = dependencyPropertyChangedEventArgs.NewValue as IComparable; switch (iComparable.CompareTo(dependencyPropertyChangedEventArgs.OldValue)) { case 1: // Indicate that the value has increased. valueBlock.SetValue(ValueBlock.IsUpProperty, true); valueBlock.SetValue(ValueBlock.IsDownProperty, false); // Raise an event that indicates the value has increased. valueBlock.RaiseEvent(new RoutedEventArgs(ValueBlock.IncreaseEvent)); break; case -1: // Indicate that the value has decreased. valueBlock.SetValue(ValueBlock.IsUpProperty, false); valueBlock.SetValue(ValueBlock.IsDownProperty, true); // Raise an event that indicates the value has decreased. valueBlock.RaiseEvent(new RoutedEventArgs(ValueBlock.DecreaseEvent)); break; } } } }