I am switching from Qt to WPF and what I want is to create a simple application containing both menu bar and toolbar. All commands are present in the menu bar and some of them are accessible also from the toolbar. Also I want to assign some icons to the commonly used commands and display them in both menu and toolbar.
I defined several custom commands in the code-behind file and now I am dealing with the XAML file. When I want to define a single menu item with icon, I have to write something like
<Window.Resources>
<BitmapImage x:Key="Connect" UriSource="Resources/connect.png" />
<Window.Resources>
...
<Menu>
<MenuItem Command="local:Commands.Connect">
<MenuItem.Icon>
<Image Source="{StaticResource Connect}" Width="16" />
</MenuItem.Icon>
</MenuItem>
</Menu>
The tool button for a single command is even more demanding:
<ToolBar>
<Button Command="local:Commands.Connect" >
<StackPanel Orientation="Horizontal">
<Image Source="{StaticResource Connect}" Width="32" />
<TextBlock Text="Connect..." VerticalAlignment="Center" />
</StackPanel>
</Button>
</ToolBar>
The code works, but in my opinion it is too bloated and confusing for higher amount of commands. I tried to play with ControlTemplate, but the TemplateBinding is quite limiting since it can use only existing attributes of the controls...
Is there a way how to make the XAML definitions shorter and more reusable? E.g. to make a template and then create each menu item with a single line of code?