Answer the question
In order to leave comments, you need to log in
How in WPF, when creating your own component (for example, a button), add content on top of the existing one (for example, a frame)?
Hello! I'm trying to figure out how to write my own controls in WPF. And I had the following question: is it possible to somehow overlay any new content on top of the old one? I understand that Button already has its own Border, but this question is purely academic and is provided as an example.
class CustomButton : Button
{
private readonly Border _border;
private readonly VisualCollection _visualChildren;
protected override Size ArrangeOverride(Size finalSize)
{
_border.Arrange(new Rect(0, 0, 100, 100));
return finalSize;
}
protected override Int32 VisualChildrenCount => _visualChildren.Count;
protected override Visual GetVisualChild(Int32 index) => _visualChildren[index];
public CustomButton()
{
_visualChildren = new VisualCollection(this);
_border = new Border();
_border.BorderThickness = new Thickness(2);
_border.BorderBrush = Brushes.Green;
_visualChildren.Add(_bottomRight);
}
}
class CustomButton : Button
{
private readonly Border _border;
protected override Size ArrangeOverride(Size finalSize)
{
_border.Arrange(new Rect(0, 0, 100, 100));
return finalSize;
}
public CustomButton()
{
_border = new Border();
_border.BorderThickness = new Thickness(2);
_border.BorderBrush = Brushes.Green;
base.AddVisualChild(_border);
}
}
Answer the question
In order to leave comments, you need to log in
I was also tormented by these questions. The simplest thing is to create an element in Blend by right-clicking - Edit Template->Current Template. Blend will generate XAML for the current element. And already in it you can do whatever your heart desires. Either by code or visually
Canvas will save you, won't it? Put a button inside the Canvas and draw a frame on top
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question