窗口顶部的 Xaml TabControl [英] Xaml TabControl in the top of window

查看:102
本文介绍了窗口顶部的 Xaml TabControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我对显示在图片上的 xaml-markup 设计有问题.如何将窗口按钮与标签项标题 TAB1TAB2TAB3 放在一行中?

I have a trouble with xaml-markup design that is shown on a picture. How to place window buttons in one line with tab item headers TAB1, TAB2, TAB3?

我对窗口按钮使用自定义控件,例如:

I use custom control for window buttons like:

<Border>
    <StackPanel Orientation="Horizontal">
        ... buttons ...
    </StackPanel>
</Border>

有没有人知道我如何实现这一点?

Does anyone have ideas how I can implement this?

推荐答案

您可能需要移除窗口边框并自己绘制按钮.您必须自己处理按钮点击(不要忘记,最大化也会在窗口最大化时恢复)并且还要自己处理窗口拖动!

You will probably have to remove the window border and draw the buttons yourself. You'll have to handle button clicks yourself (don't forget that maximize is also restore when the window is maximized) and also handle window dragging yourself too!

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"  
        Title="" WindowStyle="None" AllowsTransparency="True" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}"        
        >
    <Grid>
        <Grid Background="Silver">
            <TabControl>
                <TabItem Header="Tab 1"/>
                <TabItem Header="Tab 2"/>
                <TabItem Header="Tab 3"/>                
            </TabControl>
        </Grid>
        <StackPanel HorizontalAlignment="Right" VerticalAlignment="Top" Orientation="Horizontal" >
            <Button Content="_" Width="30" Command="{Binding MinimizeCommand}"/>
            <Button Content="-" Width="30" Command="{Binding MaximizeCommand}" />
            <Button Content="x" Width="30" Command="{Binding CloseCommand}"/>
        </StackPanel>        
    </Grid>
</Window>

您可以看到与按钮相关联的命令是在窗口后面的代码中定义的.

The commands that you can see hooked up to the buttons are defined in code behind in the window.

  public partial class MainWindow : Window
    {
        public ICommand CloseCommand
        {
            get { return (ICommand)GetValue(CloseCommandProperty); }
            set { SetValue(CloseCommandProperty, value); }
        }
        public ICommand MinimizeCommand
        {
            get { return (ICommand)GetValue(MinimizeCommandProperty); }
            set { SetValue(MinimizeCommandProperty, value); }
        }
        public ICommand MaximizeCommand
        {
            get { return (ICommand)GetValue(MaximizeCommandProperty); }
            set { SetValue(MaximizeCommandProperty, value); }
        }

        public static readonly DependencyProperty CloseCommandProperty = DependencyProperty.Register("CloseCommand", typeof(ICommand), typeof(MainWindow), new PropertyMetadata(null));
        public static readonly DependencyProperty MinimizeCommandProperty = DependencyProperty.Register("MinimizeCommand", typeof(ICommand), typeof(MainWindow), new PropertyMetadata(null));
        public static readonly DependencyProperty MaximizeCommandProperty = DependencyProperty.Register("MaximizeCommand", typeof(ICommand), typeof(MainWindow), new PropertyMetadata(null));

        public MainWindow()
        {
            InitializeComponent();
            System.Windows.Interactivity.EventObserver a;


            // Setup the commands.
            CloseCommand = new RoutedCommand("CloseCommand", typeof(MainWindow));
            MinimizeCommand = new RoutedCommand("MinimizeCommand", typeof(MainWindow));
            MaximizeCommand = new RoutedCommand("MaximizeCommand", typeof(MainWindow));

            // Put them in the windows command bindings.
            this.CommandBindings.Add(new CommandBinding(CloseCommand, new ExecutedRoutedEventHandler((s, e) => this.Close()), new CanExecuteRoutedEventHandler((s, e) => { e.CanExecute = true; })));
            this.CommandBindings.Add(new CommandBinding(MinimizeCommand, new ExecutedRoutedEventHandler((s, e) => this.WindowState = System.Windows.WindowState.Minimized), new CanExecuteRoutedEventHandler((s, e) => { e.CanExecute = true; })));
            this.CommandBindings.Add(new CommandBinding(MaximizeCommand, new ExecutedRoutedEventHandler((s, e) => this.WindowState = System.Windows.WindowState.Maximized), new CanExecuteRoutedEventHandler((s, e) => { e.CanExecute = true; })));
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                DragMove();

            base.OnMouseMove(e);
        }
}

这篇关于窗口顶部的 Xaml TabControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆