C# Mahapp更改LeftWindow更改选项卡时的命令项
我在主窗口中有一个tabControl和一个正在工作的LeftWindowCommandC# Mahapp更改LeftWindow更改选项卡时的命令项,c#,wpf,mahapps.metro,C#,Wpf,Mahapps.metro,我在主窗口中有一个tabControl和一个正在工作的LeftWindowCommand <Controls:MetroWindow.LeftWindowCommands> <Controls:WindowCommands > <Button x:FieldModifier="public" x:Name="btnOpenBanMenu" Click="btnOpenBanMenu_Click"> <Sta
<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>
MainViewModel:
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;
}
}
理想情况下,您可以使用绑定,但由于您使用的是代码隐藏,因此这里有一个简单的解决方案(如果您希望将其调整为某种模式,如MVVM,则由您决定): 该代码的基本功能是:
- 有一个
包含所有子菜单的列表
(它们可以是任何东西,从简单的ui元素
到充满元素的按钮
堆栈面板)
- 重要提示:列表中的项目必须排序,这意味着索引0=>选项卡索引0的子菜单
- 在
中有一个WindowCommands
将负责包含子菜单transitionContentControl
- 每次选定的选项卡更改时,我都会将
列表的n位置加载到
(n是TransitioningContentControl
选项卡控件的选定索引)
如果您尝试将其调整到MVVM,并且遇到任何问题,我会在这里提供帮助。您不希望子菜单成为一个
ItemsControl
绑定到所选选项卡上的属性,该属性包含选项卡的命令吗?这样,每当所选选项卡更改时,子菜单中就会充满这些命令。是的,我用mvvm尝试过。你可以在我的第一篇文章中找到所有内容。它与MVVM一起工作。但是我仍然不知道,如何绑定leftWindow命令。你能给我一个提示吗?我也用UIElement而不是同样结构的WindowCommands进行了尝试,但效果并不理想。(看我的编辑2)
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;
}
}
<Controls:MetroWindow.DataContext>
<ViewModels:MainViewModel/>
</Controls:MetroWindow.DataContext>
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Header}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<Controls:MetroContentControl
Content="{Binding Content}" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
<Controls:MetroWindow.LeftWindowCommands>
<Controls:WindowCommands ItemsSource="{Binding LeftWindowCommands}">
</Controls:WindowCommands>
</Controls:MetroWindow.LeftWindowCommands>
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;
}
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];
}
}
}
<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>
using MahApps.Metro.Controls;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;