I need to create a new control which is composed of a TextBox and a Button. The most concise way I’ve found at the moment is with a CusomControl like this:
-
inherit from TextBox and add the functionality of the command that must happen with the Button
public class MyTextBoxWithButton : TextBox { ... DefaultStyleKeyProperty.OverrideMetadata(typeof(MyTextBoxWithButton), new FrameworkPropertyMetadata(typeof(MyTextBoxWithButton))); ... public SomeBehaviourForTheButtonCommand(){...} }
in a ResourceDictionary, clone ALL the default TextBox template and replace the border with something like
<DockPanel>
<Border>
<Button>
</DockPanel>
the part I don’t like is having cloned the ENTIRE TextBox default template.
I don’t like the unnecessary duplication and also the additional work if in the future I want to add e.g. dark theme.
I can reduce the duplication a bit by extending the type style
<Style BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type local:MyTextBoxWithButton}">
With this I can avoid some initial property setters but the definition of the ControlTemplate remains entirely duplicated.
The other solution would be to implement a UserControl, but the cost of the boiler plate on the DependencyProperty seems equally high and ugly to me.
In general is there a way to create a control while minimizing code and xaml duplication?