WPF 40 px 总是溢出右侧窗口

问题描述 投票:0回答:1

在我的 WPF 应用程序中,我有一个自己创建的小部件 (

Gantt
)。

这是

InitializeUI()
主窗口中的
Gantt

private void InitializeUI() {
            canvas = new Canvas
        {
            Background = Brushes.White,
        };

        // Bind canvas width and height to the width and height of the window
        BindingOperations.SetBinding(canvas, Canvas.WidthProperty, new Binding("ActualWidth") { Source = this });
        BindingOperations.SetBinding(canvas, Canvas.HeightProperty, new Binding("ActualHeight") { Source = this });

        // Set the Border as the content of the window
        Content = canvas;

        Gantt gantt = new Gantt();
        canvas.Children.Add(gantt);

        BindingOperations.SetBinding(gantt, Gantt.WidthProperty, new Binding("ActualWidth") { Source = canvas });
        BindingOperations.SetBinding(gantt, Gantt.HeightProperty, new Binding("ActualHeight") { Source = canvas });
}

这是甘特图的布局例程:

private void createTreeView()
{
    this.treeView = new TreeView();
    this.treeView.BorderThickness = new Thickness(0);
    this.treeView.Margin = new Thickness(0);
    this.gantt_toolbox = new Gantt_Toolbox();


    this.grid = new Grid();
    // Define column definitions
    this.grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });

    // Define row definitions
    this.grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
    this.grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
    this.grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });

    this.grid.VerticalAlignment = VerticalAlignment.Stretch;
    this.grid.HorizontalAlignment = HorizontalAlignment.Stretch;

    Grid.SetRow(this.gantt_toolbox, 0);
    Grid.SetColumn(this.gantt_toolbox, 0);

    Grid.SetRow(this.treeView, 1);
    Grid.SetColumn(this.treeView, 0);

    Grid.SetRow(this.mainTimeScrollbar, 2);
    Grid.SetColumn(this.mainTimeScrollbar, 0);

    this.grid.Children.Add(this.gantt_toolbox);
    this.grid.Children.Add(this.treeView);
    this.grid.Children.Add(this.mainTimeScrollbar);

    this.Content = this.grid;
}

我的问题是,在右侧,有大约 40 px 的差异,因为滚动条的右侧按钮不可见。 还有顶部的按钮,右侧的按钮,有一个 40 px 的缺失区域。

enter image description here

我用 debug 检查过,

Gantt
Canvas
Grid
具有相同的
ActualWidth
属性。

我有什么错?

wpf wpf-controls
1个回答
0
投票

默认情况下,窗口由两个重叠的矩形组成。内部矩形是绘制应用程序内容的客户区。外部矩形是非客户区,由标题栏和边框组成,包围客户区。请参阅标准窗口

Standard window of Windows 7

Window.Width和Window.ActualWidth表示非客户区的宽度,由于两个矩形之间有间隙,因此它们比客户区的宽度大。从 Windows 10 开始,非客户区的边框已经消失,除了标题栏和细长条外,外部和内部矩形看起来是匹配的,但实际上,间隙仍然存在。

话虽如此,您不需要设置绑定以使其宽度与客户区域的宽度匹配,因为 Canvas.HorizontalAlignment 的默认值为 Stretch。

© www.soinside.com 2019 - 2024. All rights reserved.