首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF MahApps.Metro工具栏

WPF MahApps.Metro工具栏
EN

Stack Overflow用户
提问于 2016-02-07 05:25:41
回答 1查看 2.6K关注 0票数 1

我想在带有MahApps.Metro的WPF中使用ToolBar 'User control‘

用户控制:

代码语言:javascript
复制
<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">

窗口:

代码语言:javascript
复制
<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

EN

回答 1

Stack Overflow用户

发布于 2016-02-19 06:39:39

这里有两种获得你想要的东西的方法。

对资源字典中定义的ControlTemplate使用ContentControl

代码语言:javascript
复制
<ControlTemplate x:Key="ToolBarTemplate">
    <ToolBarTray>
        <ToolBar>
            <Button Content="Add"/>
            <Button Content="Delete"/>
            <Button Content="Save"/>
            <Button Content="Cancel"/>
        </ToolBar>
    </ToolBarTray>
</ControlTemplate>

现在,您可以在需要的地方使用此模板。

代码语言:javascript
复制
<ContentControl Template="{DynamicResource ToolBarTemplate}"
                Content="{Binding ViewModel4ToolBar}" />

或者使用UserControl

代码语言:javascript
复制
<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>

用法

代码语言:javascript
复制
<local:ToolBarUserControl DataContext="{Binding ViewModel4ToolBar}" />

希望这能有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35246649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档