implemented context menu that opens when the AddTab Button is pressed

master
Jonas Arnold 3 years ago
parent b0e63d0437
commit c55411d6c9
  1. 2
      MultiTerm.Core/ViewModel/ShellViewModel.cs
  2. 38
      MultiTerm.Wpf.CustomControl/ExtendedTabControl/ExtendedTabControl.cs
  3. 2
      MultiTerm.Wpf.CustomControl/ExtendedTabControl/ExtendedTabControl.xaml
  4. 115
      MultiTerm.Wpf/View/ShellView.xaml

@ -39,6 +39,8 @@ public partial class ShellViewModel : ObservableObject
public ShellViewModel(ITerminalViewModelFactory terminalViewModelFactory) public ShellViewModel(ITerminalViewModelFactory terminalViewModelFactory)
{ {
this.terminalViewModelFactory = terminalViewModelFactory; this.terminalViewModelFactory = terminalViewModelFactory;
// TEMP Init
this.AppendTerminalWithSelectedViewType(ProtocolType.Serial);
} }
[RelayCommand] [RelayCommand]

@ -1,4 +1,5 @@
using System.Windows; using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
namespace MultiTerm.Wpf.CustomControl namespace MultiTerm.Wpf.CustomControl
@ -34,9 +35,44 @@ namespace MultiTerm.Wpf.CustomControl
/// </summary> /// </summary>
public class ExtendedTabControl : TabControl public class ExtendedTabControl : TabControl
{ {
public static readonly RoutedEvent AddButtonClickedEvent;
#region Dotnet Properties
/// <summary>
/// .NET Property for <see cref="AddButtonClickedEvent"/>
/// </summary>
public event RoutedEventHandler AddButtonClicked
{
add { this.AddHandler(AddButtonClickedEvent, value); }
remove { this.RemoveHandler(AddButtonClickedEvent, value); }
}
#endregion
static ExtendedTabControl() static ExtendedTabControl()
{ {
DefaultStyleKeyProperty.OverrideMetadata(typeof(ExtendedTabControl), new FrameworkPropertyMetadata(typeof(ExtendedTabControl))); DefaultStyleKeyProperty.OverrideMetadata(typeof(ExtendedTabControl), new FrameworkPropertyMetadata(typeof(ExtendedTabControl)));
AddButtonClickedEvent = EventManager.RegisterRoutedEvent("AddButtonClicked",
RoutingStrategy.Bubble, typeof(RoutedEventArgs),
typeof(ExtendedTabControl));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
// get button from template
var button = GetTemplateChild("addButton") as Button;
if (button != null)
{
button.Click += OnAddButtonClicked; ;
}
}
private void OnAddButtonClicked(object sender, RoutedEventArgs e)
{
RoutedEventArgs args = new RoutedEventArgs(AddButtonClickedEvent);
RaiseEvent(args);
} }
} }
} }

@ -26,7 +26,7 @@
</Grid.RowDefinitions> </Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0"> <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
<TabPanel x:Name="headerPanel" Background="Transparent" IsItemsHost="true" Margin="2,2,2,0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/> <TabPanel x:Name="headerPanel" Background="Transparent" IsItemsHost="true" Margin="2,2,2,0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
<Button Margin="2,2,2,2" Height="{TemplateBinding Height}" Width="{TemplateBinding Height}">+</Button> <Button x:Name="addButton" FontSize="16" Height="{Binding ElementName=headerPanel, Path=Height}" Width="{Binding ElementName=headerPanel, Path=Height}"> + </Button>
</StackPanel> </StackPanel>
<Border x:Name="contentPanel" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local"> <Border x:Name="contentPanel" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
<ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>

@ -39,7 +39,6 @@
<x:Type TypeName="core_common:ProtocolType" /> <x:Type TypeName="core_common:ProtocolType" />
</ObjectDataProvider.MethodParameters> </ObjectDataProvider.MethodParameters>
</ObjectDataProvider> </ObjectDataProvider>
</UserControl.Resources> </UserControl.Resources>
<DockPanel> <DockPanel>
@ -73,14 +72,32 @@
<MenuItem Header="_View"/> <MenuItem Header="_View"/>
<MenuItem Header="_About"/> <MenuItem Header="_About"/>
</Menu> </Menu>
<!-- Left Panel that holds History and Command Sets -->
<StackPanel Orientation="Vertical" DockPanel.Dock="Left" Width="150"> <StackPanel Orientation="Vertical" DockPanel.Dock="Left" Width="150">
<Button Name="AddNewTabButton" Content="Add new Tab">
<Button.Resources> </StackPanel>
<!-- Binding Proxy to provide DataContext to elements within ContextMenu -->
<helpers:BindingProxy x:Key="proxy" Data="{Binding}"/> <!-- Grid to contain hidden add Tab Button, and the tab control -->
</Button.Resources> <Grid DockPanel.Dock="Right">
<Grid.RowDefinitions>
<RowDefinition Height="0"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.Resources>
<!-- Binding Proxy to provide DataContext to elements within ContextMenu -->
<helpers:BindingProxy x:Key="proxy" Data="{Binding}"/>
</Grid.Resources>
<!-- Button with context menu that is never visible -->
<Button Visibility="Collapsed" Grid.Row="0" Grid.Column="0">
<Button.ContextMenu> <Button.ContextMenu>
<ContextMenu x:Name="MainContextMenu" PlacementRectangle="{Binding RelativeSource={RelativeSource Self}}"> <ContextMenu x:Name="AddTabContextMenu" PlacementRectangle="{Binding RelativeSource={RelativeSource Self}}">
<!-- View types --> <!-- View types -->
<controls:SingleSelectSubMenu Title="View type" <controls:SingleSelectSubMenu Title="View type"
OptionsSource="{Binding Source={StaticResource TerminalViewTypeValues}}" OptionsSource="{Binding Source={StaticResource TerminalViewTypeValues}}"
@ -98,12 +115,44 @@
</controls:CommandableSubMenu> </controls:CommandableSubMenu>
</ContextMenu> </ContextMenu>
</Button.ContextMenu> </Button.ContextMenu>
</Button>
<custom_controls:ExtendedTabControl Grid.Row="1" Grid.Column="1"
x:Name="terminalTabControl"
ItemsSource="{Binding TerminalViewModels}"
SelectedItem="{Binding SelectedTerminalViewModel}">
<Button.Triggers> <!-- Register additional Tab ViewModels here -->
<EventTrigger SourceName="AddNewTabButton" RoutedEvent="Button.Click"> <custom_controls:ExtendedTabControl.Resources>
<DataTemplate DataType="{x:Type vm:SendReceiveViewModel}">
<v:SendReceiveView/>
</DataTemplate>
</custom_controls:ExtendedTabControl.Resources>
<custom_controls:ExtendedTabControl.ItemTemplate>
<!-- Tab Template -->
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title, Mode=OneWay}" />
<Button Command="{Binding CloseRequestCommand}" Width="20" Padding="0" Margin="8 0 0 0" Content="X">
<Button.Style>
<Style TargetType="Button" x:Name="CloseButtonStyle">
<Setter Property="Visibility" Value="Visible"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Style>
</Button.Style>
</Button>
</StackPanel>
</DataTemplate>
</custom_controls:ExtendedTabControl.ItemTemplate>
<custom_controls:ExtendedTabControl.Triggers>
<!-- Trigger that opens the context menu, listening on custom routed event AddButtonClicked -->
<EventTrigger RoutedEvent="custom_controls:ExtendedTabControl.AddButtonClicked">
<BeginStoryboard> <BeginStoryboard>
<Storyboard> <Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainContextMenu" Storyboard.TargetProperty="(ContextMenu.IsOpen)"> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="AddTabContextMenu" Storyboard.TargetProperty="(ContextMenu.IsOpen)">
<DiscreteObjectKeyFrame KeyTime="0:0:0"> <DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value> <DiscreteObjectKeyFrame.Value>
<sys:Boolean>True</sys:Boolean> <sys:Boolean>True</sys:Boolean>
@ -113,48 +162,8 @@
</Storyboard> </Storyboard>
</BeginStoryboard> </BeginStoryboard>
</EventTrigger> </EventTrigger>
</Button.Triggers> </custom_controls:ExtendedTabControl.Triggers>
</Button> </custom_controls:ExtendedTabControl>
</Grid>
<!--<RibbonMenuButton x:Name="newTerminalMenu" Label="Add new Tab" Background="Gray">
<RibbonRadioButton Label="SendReceive">
</RibbonRadioButton>
<RibbonRadioButton Label="Console">
</RibbonRadioButton>
<RibbonSeparator/>
</RibbonMenuButton>-->
</StackPanel>
<custom_controls:ExtendedTabControl DockPanel.Dock="Right"
x:Name="terminalTabControl"
ItemsSource="{Binding TerminalViewModels}"
SelectedItem="{Binding SelectedTerminalViewModel}">
<!-- Tab Template -->
<custom_controls:ExtendedTabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title, Mode=OneWay}" />
<Button Command="{Binding CloseRequestCommand}" Width="20" Padding="0" Margin="8 0 0 0" Content="X">
<Button.Style>
<Style TargetType="Button" x:Name="CloseButtonStyle">
<Setter Property="Visibility" Value="Visible"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Style>
</Button.Style>
</Button>
</StackPanel>
</DataTemplate>
</custom_controls:ExtendedTabControl.ItemTemplate>
<!-- Register additional Tab ViewModels here -->
<custom_controls:ExtendedTabControl.Resources>
<DataTemplate DataType="{x:Type vm:SendReceiveViewModel}">
<v:SendReceiveView/>
</DataTemplate>
</custom_controls:ExtendedTabControl.Resources>
</custom_controls:ExtendedTabControl>
</DockPanel> </DockPanel>
</UserControl> </UserControl>

Loading…
Cancel
Save