MahApps:Metro SplitButton Control Template

857 Views Asked by At

Default SplitButton works ok, but when I try to work with it's control template issues arise. If I try to get Control Template with reflection (with ConstructorInfo) I get empty Control Template for SplitButton. If I try to 'Edit Template copy' in XAML Designer, I get copy which does not work (like ItemsSource does not bind to elements in ListBox of SplitButton as it is always empty). My version of MahApps Metro is 1.4.3.0

Here is how I try to get Control Template of SplitButton:

MahApps.Metro.Controls.SplitButton ctl  = sender as MahApps.Metro.Controls.SplitButton;

Type type = ctl.GetType();

if (type == null)
    return;

// Instantiate the type.
ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes);
Control control = (Control)info.Invoke(null);

// Get the template.
ControlTemplate template = control.Template;

// Get the XAML for the template.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, settings);
XamlWriter.Save(template, writer);
2

There are 2 best solutions below

2
On

The default ControlTemplate is available on GitHub: https://github.com/MahApps/MahApps.Metro/blob/336f7dfc4bda2d0eba8aa270737ca3c11d45128c/src/MahApps.Metro/MahApps.Metro/Themes/SplitButton.xaml

MahApps.Metro is open source so you can download the source code if you want to.

1
On

After updating MahApps Metro to 1.5.0 SplitButton works fine with provided Control Templates...