如何将TabControl标题链接到调用MenuItem标题 [英] How do I Link A TabControl Header to calling MenuItem Header
本文介绍了如何将TabControl标题链接到调用MenuItem标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我已经决定试用WPF而且我一直坚持命名tabcontrol标题。使用Josh的帖子,我的愿望是每次用户点击菜单时都会创建一个Tab,并且标签的标题将被设置为调用menuitem标题。我怎样才能以最简单的方式实现这一目标。请告知我应该使用哪个功能。 Thanx提前!
Stan
解决方案
菜单xaml
< 菜单 名称 = mnuMain DockPanel.Dock = 顶部 HorizontalAlignment = 拉伸 宽度 = 自动 MenuItem.Click = menuClick >
< MenuItem 标题 = _File >
< MenuItem 命令 = {绑定NewWorkspaceCommand} 标题 = _新标签 InputGestureText = Ctrl + N / >
< MenuItem 命令 = {Binding CloseWorkspaceCommand} 标题 = _关闭标签 InputGestureText = Ctrl + F4 / >
< 我nuItem 命令 = {Binding ExitCommand} 标题 = E_xit InputGestureText = Ctrl + X / >
< / MenuItem >
标签控制如下
< TabControl.Background >
< ImageBrush < span class =code-attribute> ImageSource = / Resources / images / background.png 拉伸 = UniformToFill / >
< / TabControl.Background >
< TabControl.ItemTemplate >
< DataTemplate >
< WrapPanel >
< TextBlock 文字 = {Binding Header} / >
< 按钮 命令 = {Binding CloseCommand} 内容 = X 保证金 = 4,0,0,0 FontFamily = Courier New 宽度 = < span class =code-keyword> 17 高度 = 17 VerticalContentAlignment = 中心 / >
< / WrapPanel >
< span class =code-keyword>< / DataTemplate >
< / TabControl.ItemTemplate > ;
< / TabControl >
viewmodel
public class MainViewModel:ViewModelBase
{
#region构造函数
public MainViewModel()
{
Workspaces = new ObservableCollection< workspaceviewmodel>();
Workspaces.CollectionChanged + = Workspaces_CollectionChanged;
}
#endregion
< span class =code-region> #region事件处理程序
public void menuClick( object sender,RoutedEventArgs e)
{
MenuItem item = e.OriginalSource as MenuItem;
if ( null != item)
{
CallingMenuName = item.Name; {}
}
}
void Workspaces_CollectionChanged( object sender,NotifyCollectionChangedEventArgs e)
{
if (e.NewItems!= null && e.NewItems.Count!= 0 )
foreach ( e.NewItems中的WorkspaceViewModel工作区)
workspace.RequestClose + = this 跨度> .OnWorkspaceRequestClose;
if (e.OldItems!= null && e。 OldItems.Count!= 0 )
foreach (WorkspaceViewModel工作区 in e.OldItems)
workspace.RequestClose - = this .OnWorkspaceRequestClose;
}
private void OnWorkspaceRequestClose( object sender,EventArgs e)
{
CloseWorkspace();
}
#endregion
#region命令
private DelegateCommand _exitCommand;
public ICommand ExitCommand
{
get {返回 _exitCommand ?? (_exitCommand = new DelegateCommand(()= > Application.Current.Shutdown())); }
}
private DelegateCommand _newWorkspaceCommand;
public ICommand NewWorkspaceCommand
{
get {返回 _newWorkspaceCommand ?? (_newWorkspaceCommand = new DelegateCommand(NewWorkspace)); }
}
私有 void NewWorkspace()
{
frmCustomer oCustomer = new frmCustomer();
var workspace = new WorkspaceViewModel {Header = 新标签};
Workspaces.Add(工作区);
/// = oCustomer.Content;
SelectedIndex = Workspaces.IndexOf(工作区);
}
private DelegateCommand _closeWorkspaceCommand;
public ICommand CloseWorkspaceCommand
{
get {返回 _closeWorkspaceCommand ?? (_closeWorkspaceCommand = new DelegateCommand(CloseWorkspace,()= > Workspaces.Count > 0 )); }
}
private void CloseWorkspace()
{
Workspaces.RemoveAt(SelectedIndex);
SelectedIndex = 0 ;
}
#endregion
#region Public Members
public ObservableCollection< workspaceviewmodel>工作区{获取; set ; }
private int _selectedIndex = < span class =code-digit> 0 ;
public int SelectedIndex
{
get { return _selectedIndex; }
set
{
_selectedIndex = value ;
OnPropertyChanged( SelectedIndex);
}
}
私人 字符串 _CallingMenuName = ;
public string CallingMenuName
{
get { return _CallingMenuName; }
set
{
_CallingMenuName = value ;
OnPropertyChanged( CallingMenuName);
}
}
#endregion
}
}
Hi Guys,
I have decided to try out WPF and I am stuck on naming the tabcontrol header. Using Josh's post my wish is to a have Tab created everytime a user clicks a menu and the tab's header will be set to the calling menuitem header. How can I accomplish this in the simplest way. Please advice on which feature I should use. Thanx in advance!
Stan
解决方案
menu xaml
<Menu Name="mnuMain" DockPanel.Dock="Top" HorizontalAlignment="Stretch" Width=" Auto" MenuItem.Click ="menuClick"> <MenuItem Header="_File"> <MenuItem Command="{Binding NewWorkspaceCommand}" Header="_New Tab" InputGestureText="Ctrl + N" /> <MenuItem Command="{Binding CloseWorkspaceCommand}" Header="_Close Tab" InputGestureText="Ctrl + F4" /> <MenuItem Command="{Binding ExitCommand}" Header="E_xit" InputGestureText="Ctrl + X" /> </MenuItem>
tab control is as follows
<TabControl.Background > <ImageBrush ImageSource="/Resources/images/background.png" Stretch="UniformToFill" /> </TabControl.Background> <TabControl.ItemTemplate> <DataTemplate> <WrapPanel> <TextBlock Text="{Binding Header}" /> <Button Command="{Binding CloseCommand}" Content="X" Margin="4,0,0,0" FontFamily="Courier New" Width="17" Height="17" VerticalContentAlignment="Center" /> </WrapPanel> </DataTemplate> </TabControl.ItemTemplate> </TabControl>
viewmodel
public class MainViewModel : ViewModelBase { #region Constructor public MainViewModel() { Workspaces = new ObservableCollection<workspaceviewmodel>(); Workspaces.CollectionChanged += Workspaces_CollectionChanged; } #endregion #region Event Handlers public void menuClick(object sender, RoutedEventArgs e) { MenuItem item = e.OriginalSource as MenuItem; if (null != item) { CallingMenuName = item.Name;{} } } void Workspaces_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.NewItems != null && e.NewItems.Count != 0) foreach (WorkspaceViewModel workspace in e.NewItems) workspace.RequestClose += this.OnWorkspaceRequestClose; if (e.OldItems != null && e.OldItems.Count != 0) foreach (WorkspaceViewModel workspace in e.OldItems) workspace.RequestClose -= this.OnWorkspaceRequestClose; } private void OnWorkspaceRequestClose(object sender, EventArgs e) { CloseWorkspace(); } #endregion #region Commands private DelegateCommand _exitCommand; public ICommand ExitCommand { get { return _exitCommand ?? (_exitCommand = new DelegateCommand(() => Application.Current.Shutdown())); } } private DelegateCommand _newWorkspaceCommand; public ICommand NewWorkspaceCommand { get { return _newWorkspaceCommand ?? (_newWorkspaceCommand = new DelegateCommand(NewWorkspace)); } } private void NewWorkspace() { frmCustomer oCustomer = new frmCustomer(); var workspace = new WorkspaceViewModel { Header = "New Tab" } ; Workspaces.Add(workspace); /// = oCustomer.Content; SelectedIndex = Workspaces.IndexOf(workspace); } private DelegateCommand _closeWorkspaceCommand; public ICommand CloseWorkspaceCommand { get { return _closeWorkspaceCommand ?? (_closeWorkspaceCommand = new DelegateCommand(CloseWorkspace, () => Workspaces.Count > 0)); } } private void CloseWorkspace() { Workspaces.RemoveAt(SelectedIndex); SelectedIndex = 0; } #endregion #region Public Members public ObservableCollection<workspaceviewmodel> Workspaces { get; set; } private int _selectedIndex = 0; public int SelectedIndex { get { return _selectedIndex; } set { _selectedIndex = value; OnPropertyChanged("SelectedIndex"); } } private string _CallingMenuName = ""; public string CallingMenuName { get { return _CallingMenuName; } set { _CallingMenuName = value; OnPropertyChanged("CallingMenuName"); } } #endregion } }
这篇关于如何将TabControl标题链接到调用MenuItem标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文