2017-01-10 10 views
1

を変更するとき、私はそれがすべての罰金です私のメインウィンドウでTabControlのと作業LeftWindowCommandMahApps変更LeftWindowCommands項目タブ

<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を "Sub-Menu-Bar"として使用したいので、選択したTabを変更した場合、LeftWindowCommands-Barは項目やボタンの背後にあるアクションも変更する必要があります。 私は視界で遊んだことがありますが、それは私が望むものではありません。

より良く理解するために:

enter image description here


あなたはアイテム "プレゼント、Losung、Songrequest" を参照してください。 これらのアイテムは私のTabControlの中にあります。

そして今、私は別のタブを選んでプレゼントをするときに、「サブメニュー」 - 項目(画像に記載)を変更したいと思います。

誰かが私の手引きをしてくれますか?


EDIT2:最後にMVVMで動作しますが、LeftWindowCommandsをバインドする方法はまだわかりません。

MainModel:

class MainModel 
{ 
    public string Header { get; set; } 
    public MahApps.Metro.Controls.MetroContentControl Content { get; set; } 
    public MahApps.Metro.Controls.WindowCommands LeftWindowCommands { get; set; } 
} 

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; 
    } 
} 

のDataContext:

<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> 

これは動作します。デザイナーはすべてのタブ&の内容を表示します。 どのように私はWindowCommandsをバインドできますか?

<Controls:MetroWindow.LeftWindowCommands> 
    <Controls:WindowCommands ItemsSource="{Binding LeftWindowCommands}"> 
    </Controls:WindowCommands> 
</Controls:MetroWindow.LeftWindowCommands> 

はさらに私は私のMainViewModelに複数のボタンを追加できるようにしたい: は、私のような何かをしたいです。何かのように:理想的

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; 
    } 
+0

SubMenuには、タブのコマンドが含まれている選択したタブのプロパティにバインドされた 'ItemsControl'がありますか?そのようにして、選択したタブが変わるたびに、SubMenuはそれらのコマンドでいっぱいになります。 –

答えて

1

、あなたはバインディングを使用していますが、コードビハインドを使用しているので、ここでは簡単なソリューションです(あなたはMVVMのようないくつかのパターに適合するようにしたい場合は、それはあなた次第です):

は基本的に何このコードがないことである:

  • は、すべてのサブメニューを含むUIElementsListをあります(彼らは、要素の完全なStackPanelにシンプルButtonから、何もすることができます)。

    • 重要:リストの項目はタブインデックス0のインデックス0 =>サブメニューを意味し、注文する必要があります。WindowCommandsインサイド
  • サブメニューを収容するための責任を負うことになりますTransitioningContentControlがあります。

  • 毎回選択されたタブの変更は、私はTransitioningContentControlListのn個の位置を荷重(N TabControlの選択されたインデックスです)。出力

:あなたはそれを適応させることができますので、ここで

Output

とは、私が例で使用したコードです:

コードビハインド:

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; 

をあなたはMVVMにそれを適応し、私は助けるためにここにいるすべてのトラブルを持ってしようとします。

+0

ありがとう、完璧な作品! – Mojack

+0

ええ、私はmvvmで試しました。あなたは私の最初の投稿のすべてを見つける。 – Mojack

+0

MVVMで動作します。しかし、私はまだ知りません、どのようにLeftWindowCommandsをバインドする。あなたは私にヒントを与えることができますか?私は同じ構造体を持つWindowCommandsの代わりにUIElementを使ってみましたが、これもうまくいきませんでした。 (私のEDIT2を見てください) – Mojack

関連する問題