我有一个xaml-markup
设计,显示在图片上的麻烦。如何将窗口按钮与选项卡项目标题放在一行TAB1
,TAB2
,TAB3
?
我用像窗口按钮自定义控件:
<Border>
<StackPanel Orientation="Horizontal">
... buttons ...
</StackPanel>
</Border>
任何人都不会有想法如何,我可以实现这一点?
我有一个xaml-markup
设计,显示在图片上的麻烦。如何将窗口按钮与选项卡项目标题放在一行TAB1
,TAB2
,TAB3
?
我用像窗口按钮自定义控件:
<Border>
<StackPanel Orientation="Horizontal">
... buttons ...
</StackPanel>
</Border>
任何人都不会有想法如何,我可以实现这一点?
您可能必须删除窗口边框并自己绘制按钮。你必须自己处理按钮点击(不要忘记,当窗口最大化时,最大化也会恢复),并且还可以自己处理窗口拖动!
<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>
,你可以看到挂在按钮的命令在代码中定义后面的窗口。
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);
}
}
有一个从微软现已解散的项目叫WPF Shell Integration library,它可以让你绘制WPF看中玻璃窗,与标签,进入标题栏区域。不幸的是,它并不完美。
WPF SDK的Microsoft功能区包含最新版本。这就是RibbonWindow如何将功能区合并到标题栏区域(如Office)的方式。
+1,尽管我会使用Click事件而不是命令来处理窗口管理代码,因为它更简单:) – Rachel
更容易,直到您开始查看mvvm并且后面的代码不知道UI的外观如何。 – Andy
使用MVVM中的代码隐藏技术,您只需提供用于UI的代码即可,并且没有业务/应用程序逻辑。我会考虑将窗口操作为UI特定的,所以在使用MVVM时将代码隐藏在代码后面是可行的:) – Rachel