/// <summary> /// Handles changes to <see cref="ProtectedString"/>. /// </summary> /// <param name="o">This control.</param> /// <param name="e">EventArgs for the property change.</param> private static void ProtectedStringChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { ProtectedTextBox thisControl = (ProtectedTextBox)o; IProtectedString oldStr = (IProtectedString)e.OldValue; if (oldStr != null) { oldStr.PropertyChanged -= thisControl.WrappedStringPropertyChangeHandler; } IProtectedString newStr = (IProtectedString)e.NewValue; if (newStr != null) { newStr.PropertyChanged += thisControl.WrappedStringPropertyChangeHandler; if (newStr.Protected) { thisControl.Protect(); } else { thisControl.Deprotect(); } } }
/// <summary> /// Adjusts the size of <paramref name="item"/> based on how large it will be to fit /// the given <paramref name="str"/> within itself. /// </summary> /// <param name="item"></param> /// <param name="str"></param> public static void SizeFromProtectedString(DependencyObject item, IProtectedString str) { // Find the parent Border within the item Border templateBorder = item.FindDescendantByType <Border>(); if (templateBorder == null) { item.SetValue(VariableSizedWrapGrid.ColumnSpanProperty, 4); if (!str.Protected && str.ClearValue.Contains("\n")) { item.SetValue(VariableSizedWrapGrid.RowSpanProperty, 4); } else { item.SetValue(VariableSizedWrapGrid.RowSpanProperty, 2); } } else { templateBorder.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); TextBlock headerBlock = templateBorder.FindDescendantByType <TextBlock>(); headerBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); ProtectedTextBox contentBox = templateBorder.FindDescendantByType <ProtectedTextBox>(); contentBox.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); // As a hack I add 5px to the desiredWidth to bump it up if necessary... double desiredWidth = Math.Max(headerBlock.DesiredSize.Width, contentBox.DesiredSize.Width) + 5; double desiredHeight = headerBlock.DesiredSize.Height + contentBox.DesiredSize.Height; VariableSizedWrapGrid vswg = VisualTreeHelper.GetParent(item) as VariableSizedWrapGrid; int columns = (int)Math.Ceiling(desiredWidth / vswg.ItemWidth); columns = Math.Max(Math.Min(columns, 5), 1); item.SetValue(VariableSizedWrapGrid.ColumnSpanProperty, columns); int rows = (int)Math.Ceiling(desiredHeight / vswg.ItemHeight); rows = Math.Max(Math.Min(rows, 7), 1); item.SetValue(VariableSizedWrapGrid.RowSpanProperty, rows); } }
/// <summary> /// Handles changes to <see cref="IsReadOnly"/>. /// </summary> /// <param name="o">This control.</param> /// <param name="e">EventArgs for the property change.</param> private static void IsReadOnlyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { ProtectedTextBox thisControl = (ProtectedTextBox)o; thisControl.PART_ProtectedBox.IsReadOnly = (bool)e.NewValue; }