首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MahApps在更改选项卡时更改LeftWindowCommands项

MahApps在更改选项卡时更改LeftWindowCommands项
EN

Stack Overflow用户
提问于 2017-01-10 15:54:03
回答 1查看 1.2K关注 0票数 1

我的MainWindow里有一个MainWindow,还有一个工作LeftWindowCommand

代码语言:javascript
运行
复制
<Controls:MetroWindow.LeftWindowCommands>
    <Controls:WindowCommands >
        <Button x:FieldModifier="public" x:Name="btnOpenBanMenu" Click="btnOpenBanMenu_Click">
            <StackPanel Orientation="Horizontal">
                <Rectangle Width="20"
               Height="20"
               Fill="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}">
                    <Rectangle.OpacityMask>
                        <VisualBrush Stretch="Fill" Visual="{StaticResource bans}" />
                    </Rectangle.OpacityMask>
                </Rectangle>
                <TextBlock Margin="4 0 0 0"
               VerticalAlignment="Center"
               Text="Bans"/>
            </StackPanel>
        </Button> 
    </Controls:WindowCommands>
</Controls:MetroWindow.LeftWindowCommands>

一切都很好。但是现在我想使用LeftWindowCommand作为“子菜单栏”,所以如果您更改所选的选项卡,LeftWindowCommand也应该更改按钮后面的项和操作。我玩过能见度,但这不是我想要的。

为了更好地理解:

你可以看到“送礼,洛松,颂请求”的物品。这些物品在我的TabControl里。

现在,当我选择一个不同的选项卡,然后放弃时,我想更改"Sub菜单“-Items(在图片中描述)。

有人能指点我,怎么做?

EDIT2:最后,它适用于MVVM,但我仍然不知道如何绑定LeftWindowCommands。

MainModel:

代码语言:javascript
运行
复制
class MainModel
{
    public string Header { get; set; }
    public MahApps.Metro.Controls.MetroContentControl Content { get; set; }
    public MahApps.Metro.Controls.WindowCommands LeftWindowCommands { get; set; }
}

MainViewModel:

代码语言:javascript
运行
复制
class MainViewModel : BaseViewModel
{
    private ObservableCollection<Model.MainModel> _tabItems;
    public ObservableCollection<Model.MainModel> tabItems
    {
        get { return _tabItems; }
        set
        {
            _tabItems = value;
            OnPropertyChanged("tabItems");
        }

    }

    public MainViewModel()
    {
        _tabItems = new ObservableCollection<Model.MainModel>()
        {
            new Model.MainModel
            {
                Header = "Giveaway",
                Content = new Controls.ucGiveaway(),
                LeftWindowCommands = LeftWindowCommandsGiveaway()
            },
            ... etc 
        };
    }

    private MahApps.Metro.Controls.WindowCommands LeftWindowCommandsGiveaway()
    {
        MahApps.Metro.Controls.WindowCommands command = new MahApps.Metro.Controls.WindowCommands();
        command.Items.Add(
            new Button { Content = "MyButton #1", Foreground = Brushes.Red });

        return command;
    }
}

DataContext:

代码语言:javascript
运行
复制
<Controls:MetroWindow.DataContext>
    <ViewModels:MainViewModel/>
</Controls:MetroWindow.DataContext>

TabControl:

代码语言:javascript
运行
复制
            <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock
                Text="{Binding Header}" />
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <Controls:MetroContentControl
                    Content="{Binding Content}" />
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>

这个很管用。设计器显示所有选项卡和内容。如何绑定WindowCommands?我想要这样的东西:

代码语言:javascript
运行
复制
<Controls:MetroWindow.LeftWindowCommands>
    <Controls:WindowCommands ItemsSource="{Binding LeftWindowCommands}">
    </Controls:WindowCommands>
</Controls:MetroWindow.LeftWindowCommands>

此外,我希望能够在我的MainViewModel中添加多个按钮。类似于:

代码语言:javascript
运行
复制
private MahApps.Metro.Controls.WindowCommands LeftWindowCommandsGiveaway()
    {
        MahApps.Metro.Controls.WindowCommands command = new MahApps.Metro.Controls.WindowCommands();
        command.Items.Add(
            new Button { Content = "MyButton #1", Foreground = Brushes.Red });

        command.Items.Add(
            new Button { Content = "MyButton #2", Foreground = Brushes.Red });

        return command;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-10 17:47:30

理想情况下,您可以使用绑定,但是由于您使用的是代码隐藏,这里有一个简单的解决方案(如果您想要使它适应某种模式,如MVVM):

基本上,这段代码所做的是:

  • 有一个包含所有子菜单的List of UIElements (它们可以是任何东西,从简单的Button到充满元素的StackPanel )。
代码语言:javascript
运行
复制
- **IMPORTANT:** The items on the list must be ordered, which means index 0 => sub-menu for tab index 0.

  • WindowCommands内部有一个TransitioningContentControl,它将负责包含子菜单。
  • 每次所选选项卡发生变化时,我都会将List的n个位置加载到TransitioningContentControl (n是TabControl的选定索引)。

输出:

下面是我在这个例子中使用的代码,所以您可以修改它:

代码隐藏:

代码语言:javascript
运行
复制
public partial class MainWindow : MetroWindow
{
    public List<UIElement> LeftWindowCommands { get; private set; }

    public MainWindow()
    {
        InitializeComponent();

        LeftWindowCommands = new List<UIElement>();

        var StackPanelForTab1 = new StackPanel() { Orientation = Orientation.Horizontal };
        var StackPanelForTab2 = new StackPanel() { Orientation = Orientation.Horizontal };
        var StackPanelForTab3 = new StackPanel() { Orientation = Orientation.Horizontal };

        // You can add as many children as you want
        StackPanelForTab1.Children.Add(new Button { Content = "MyButton #1", Foreground = Brushes.Red });
        StackPanelForTab2.Children.Add(new Button { Content = "MyButton #2", Foreground = Brushes.Black });
        StackPanelForTab3.Children.Add(new Button { Content = "MyButton #3", Foreground = Brushes.Blue });

        // MUST add items in the right order on the list
        // MUST have the sabe amount of tabs on the TabControl and items on the list
        LeftWindowCommands.Add(StackPanelForTab1);
        LeftWindowCommands.Add(StackPanelForTab2);
        LeftWindowCommands.Add(StackPanelForTab3);
    }

    private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.Source is TabControl)
        {
            MyContentControl.Content = LeftWindowCommands[MyTabControl.SelectedIndex];
        }
    }
}

窗口:

代码语言:javascript
运行
复制
<Controls:MetroWindow x:Class="WpfTests.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:local="clr-namespace:WpfTests"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Controls:MetroWindow.LeftWindowCommands>
        <Controls:WindowCommands>
            <Controls:TransitioningContentControl x:Name="MyContentControl" />
        </Controls:WindowCommands>
    </Controls:MetroWindow.LeftWindowCommands>

    <TabControl SelectionChanged="TabControl_SelectionChanged" x:Name="MyTabControl" >
        <TabItem Header="Tab #1">
            <Label>#1</Label>
        </TabItem>
        <TabItem Header="Tab #2">
            <Label>#2</Label>
        </TabItem>
        <TabItem Header="Tab #3">
            <Label>#3</Label>
        </TabItem>
    </TabControl>
</Controls:MetroWindow>

你需要这个:

代码语言:javascript
运行
复制
using MahApps.Metro.Controls;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

如果您试图使其适应MVVM,并遇到任何麻烦,我将在这里帮助您。

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

https://stackoverflow.com/questions/41572915

复制
相关文章

相似问题

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