我想在带有MahApps.Metro的WPF中使用ToolBar 'User control‘
用户控制:
<MahApps:MetroWindow x:Class="MahAppsToolbarTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MahAppsToolbarTest="clr-namespace:MahAppsToolbarTest"
xmlns:MahApps="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
Title="MainWindow" Height="350" Width="525">窗口:
<MahApps:MetroWindow x:Class="MahAppsToolbarTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:MahAppsToolbarTest="clr-namespace:MahAppsToolbarTest" xmlns:MahApps="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" Title="MainWindow" Height="350" Width="525">
<Grid>
<ToolBarTray>
<ToolBar>
<Button Content="Test button"/>
</ToolBar>
<MahAppsToolbarTest:TestToolbar/>
</ToolBarTray>
</Grid>
</MahApps:MetroWindow>我想在多个窗口中使用相同的工具栏,而不是只复制它。
您可以看到不同之处:http://i.stack.imgur.com/tGJp0.png
发布于 2016-02-19 06:39:39
这里有两种获得你想要的东西的方法。

对资源字典中定义的ControlTemplate使用ContentControl
<ControlTemplate x:Key="ToolBarTemplate">
<ToolBarTray>
<ToolBar>
<Button Content="Add"/>
<Button Content="Delete"/>
<Button Content="Save"/>
<Button Content="Cancel"/>
</ToolBar>
</ToolBarTray>
</ControlTemplate>现在,您可以在需要的地方使用此模板。
<ContentControl Template="{DynamicResource ToolBarTemplate}"
Content="{Binding ViewModel4ToolBar}" />或者使用UserControl
<UserControl x:Class="MetroDemo.ExampleWindows.ToolBarUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<ToolBarTray>
<ToolBar>
<Button Content="Add"/>
<Button Content="Delete"/>
<Button Content="Save"/>
<Button Content="Cancel"/>
</ToolBar>
</ToolBarTray>
</Grid>
</UserControl>用法
<local:ToolBarUserControl DataContext="{Binding ViewModel4ToolBar}" />希望这能有所帮助!
https://stackoverflow.com/questions/35246649
复制相似问题