xaml wpf make展开窗口标题高度

时间:2017-04-05 16:10:59

标签: wpf xaml titlebar

有没有简单的方法来调整标题栏的高度。我有一个很长的标题,我需要整个事情可读。

以下是我的代码的开头部分:

<Window x:Class="Labels.Views.QtyChooser"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Chooser" Height="400" Width="300" WindowStyle="ToolWindow"  WindowStartupLocation="CenterScreen" >
    <Window.Resources>

1 个答案:

答案 0 :(得分:0)

我从这个链接收集了我的想法: Get Toolkit Style Window with WPF

但我改变了它,因为我没有App.Xaml文件,因为我没有严格地处理一个wpf项目。

在我的窗口标签中,我添加了这个:

Style="{DynamicResource WindowStyle}"

在Window.Resources部分,我添加了以下内容

        <Style x:Key="WindowStyle" TargetType="{x:Type Window}">
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="WindowStyle" Value="None" />
            <Setter Property="ResizeMode" Value="NoResize" />
            <Setter Property="Background" Value="White" />

        <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Window}">
                        <Border
        x:Name="WindowBorder"
        BorderBrush="Black"
        BorderThickness="1">
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop Offset="0" Color="White" />
                                    <GradientStop Offset="1" Color="#FFDADADA" />
                                </LinearGradientBrush>
                            </Border.Background>
                            <Grid
          Width="{TemplateBinding Width}"
          Height="{TemplateBinding Height}"
          MinWidth="{TemplateBinding MinWidth}"
          MinHeight="{TemplateBinding MinHeight}"
          HorizontalAlignment="Left"
          VerticalAlignment="Top"
          Cursor="Arrow">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="70" />
                                    <RowDefinition Height="400" />
                                </Grid.RowDefinitions>
                                <TextBlock
            Width="300"
            Height="400"
            Margin="0,0,0,0"
            Padding="0,0,30,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Text="{TemplateBinding Title}" 
            TextWrapping="WrapWithOverflow"/>

                                <StackPanel
            Margin="0,0"
            HorizontalAlignment="Right"
            VerticalAlignment="Top"
            Orientation="Horizontal">

                                    <Button
              x:Name="CloseButton"
              Width="25"
              Height="25"
              Click="CloseButton_Click"
              IsTabStop="False">
                                        <Path
                Width="12"
                Height="10"
                HorizontalAlignment="Right"
                VerticalAlignment="Top"
                Data="M0.5,0.5L4.5178828,0.5 6.0620003,3.125 7.4936447,
                  0.5 11.5,0.5 11.5,1.5476431 8.7425003,6.1201854 11.5,
                  10.359666 11.5,11.5 7.4941902,11.5 6.0620003,8.8740005 
                  4.5172949,11.5 0.5,11.5 0.5,10.43379 3.3059995,
                  6.1201582 0.5,1.4676378 0.5,0.5z"
                Fill="#E4FFFFFF"
                Stretch="Fill"
                Stroke="#FF535666" />
                                    </Button>
                                </StackPanel>
                                <Border
            Grid.Row="1"
            Margin="0"
            BorderBrush="Black"
            BorderThickness="1">
                                    <Grid>
                                        <Rectangle Fill="White" />
                                        <ContentPresenter Content="{TemplateBinding Content}" />
                                    </Grid>
                                </Border>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

在Code cs中我添加了这个:

private const uint WM_SYSCOMMAND = 0x112;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr
    SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

private void CloseButton_Click(object sender, RoutedEventArgs e)
{
    e.Handled = true;
    Window.GetWindow((DependencyObject)sender).Close();
}     

private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;
    Window.GetWindow((DependencyObject)sender).DragMove();
}

private void Border_MouseDown(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;
    var direction = (Direction)Enum.Parse(typeof(Direction), ((FrameworkElement)sender).Tag.ToString());
    ResizeWindow(PresentationSource.FromVisual((Visual)sender) as HwndSource, direction);
}

private void ResizeWindow(HwndSource hwndSource, Direction direction)
{
    SendMessage(hwndSource.Handle, WM_SYSCOMMAND, (int)(61440 + direction), 0);
}

private enum Direction
{
    Left = 1,
    Right = 2,
    Top = 3,
    TopLeft = 4,
    TopRight = 5,
    Bottom = 6,
    BottomLeft = 7,
    BottomRight = 8,
}