我有一個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