I am working with WPF and looking for a best approach for creating re-usable expanders that are customized. Specifically the Expander header would remain the same, the content would vary.
The Expander header would have 6 buttons all wired to the same methods, which will be enforced via an interface.
Assume this is Expander 1
And this is another expander
The actual content will be text, buttons, whatever. It just has text for demo purposes.
The expander is meant to be used within a User Control, in which I have 44 of them, and don't want to repeat the code all over the place.
At the moment I am using the UserControls like the following in the Window XAML
xmlns:customcontrols="clr-namespace:MyNamespace.Controls;assembly=MyAssembly"
And the actual usage:
<customcontrols:FlexExtend ..... />
And inside each User Control I am using expander like this
<Expander Style="{StaticResource ModToolPanelStyle}" Background="#403F3B" Name="toolExpand" Header="{x:Static properties:Resources.AdductionAbduction_Label}" Collapsed="toolExpand_Collapsed" Expanded="toolExpand_Expanded">
....... all the inside body stuff
</expander>
Right now I'm looking at having to replicate the code 44 times, one for each expander in each of the 44 user controls that contain the an expander. Is there a way in WPF to make this a custom control that would have the buttons and everything? I'm think no since it wouldn't be able to be bound there for the on click?
UPDATE:
As suggested I created a DataTemplate in a seperate XAML.
<DataTemplate x:Key="DesignExpanderHeaderTemplate">
<DockPanel>
<TextBlock Name="ModName"
Foreground="White"
Text="Balls">
</TextBlock>
<Button Name="MoveUpButton"
Content="MoveUp"
Width="80"
Height="25">
</Button>
</DockPanel>
</DataTemplate>
However now I am having issues binding the button from the user control:
var button = toolExpand.HeaderTemplate.FindName("MoveUpButton", toolExpand) as Button;
button.Click += delegate (object sender, RoutedEventArgs args)
{
MessageBox.Show("The button has been pressed");
};
The button is always null, so it is not able to find it.
This is how the XAML looks
<Expander Style="{StaticResource ModToolPanelStyle}"
Background="#403F3B"
x:Name="toolExpand"
HeaderTemplate="{StaticResource DesignExpanderHeaderTemplate}"
Collapsed="toolExpand_Collapsed"
Expanded="toolExpand_Expanded">