WPF: Getting correct Width of Control with Adorner

  • Thread starter Thread starter Chris Oertel
  • Start date Start date
C

Chris Oertel

Hi,
I wanted to create a textbox with an depending Label so that I do not have
to add a label each time I add TextBox to a window and tried the following:

public class LabelTextBox : TextBox
{
public static readonly DependencyProperty LabelPositionProperty =
DependencyProperty.Register("LabelPosition", typeof(enAdornerPosition),
typeof(LabelTextBox), new FrameworkPropertyMetadata(enAdornerPosition.Left,
new PropertyChangedCallback(OnLabelPropertyChanged)));
public static readonly DependencyProperty LabelCaptionProperty =
DependencyProperty.Register("LabelCaption", typeof(string),
typeof(LabelTextBox), new FrameworkPropertyMetadata("LabelTextBox", new
PropertyChangedCallback(OnLabelPropertyChanged)));
public static readonly DependencyProperty LabelWidthProperty =
DependencyProperty.Register("LabelWidth", typeof(double),
typeof(LabelTextBox), new FrameworkPropertyMetadata((double)90, new
PropertyChangedCallback(OnLabelPropertyChanged)));

public LabelTextBox()
{ }

public double LabelWidth
{
get { return (double)GetValue(LabelWidthProperty); }
set { SetValue(LabelWidthProperty, value); }
}

public string LabelCaption
{
get { return (string)GetValue(LabelCaptionProperty); }
set { SetValue(LabelCaptionProperty, value); }
}

public enAdornerPosition LabelPosition
{
get {return (enAdornerPosition)GetValue(LabelPositionProperty); }
set {SetValue(LabelPositionProperty, value);}
}

static void OnLabelPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
LabelTextBox soBox = (LabelTextBox)d;
AdornerLayer soAdornerLayer = AdornerLayer.GetAdornerLayer(soBox);
if (soAdornerLayer != null)
{
try { foreach (Adorner soAdorner in
soAdornerLayer.GetAdorners(soBox)) { soAdornerLayer.Remove(soAdorner); } }
catch { }
double sdWidth = soBox.LabelWidth;
TextBlock soLabel = new TextBlock() { Text =
soBox.LabelCaption, Width = sdWidth, Foreground = soBox.Foreground };
GenericAdorner soAdoner = new GenericAdorner(soBox, soLabel,
soBox.LabelPosition, sdWidth);
soAdornerLayer.Add(soAdoner);
}
}
}

This still works fine besides the effect that the adorner is not included in
the control width and resizing the control / window is still an adventure
wharf the GUI will look like afterwards.

Is there a way to correct the control width (i tried something like an new
Width Property which returns Textbox.Width+Label.Width, but this did not
work...)?

Does anybody know how to do this?

Thanx,
Chris
 
This still works fine besides the effect that the adorner is not includedin
the control width and resizing the control / window is still an adventure
wharf the GUI will look like afterwards.

This is sort of by design - adorners are "outside the normal flow" for
all purposes, including size calculations. It makes sense if you
consider that they are mainly used for things such as drawing resize
handles on controls in WPF designer; in another example, I used them
in a simple vector graphics editor to provide move/resize/rotate
handles for items on the canvas.
 
Back
Top