XAMLを使わないWPF入門34「コントロールの幅と高さ及び配置方法」

コンピュータ

Width,Heightを直値セットする場合と自動

・起動直後
image

・ウィンドウサイズを変更
image

ファイル名:NoXAML34.csproj


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>

ファイル名:App.cs


using System;
using System.Windows;

namespace NoXAML34;

public class App : Application
{
    [STAThread]
    public static void Main(string[] args)
    {
        var app = new App();
        app.Run();
    }

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        var win = new MainWindow();
        win.Show();
    }
}

ファイル名:AssemblyInfo.cs


using System.Windows;

[assembly:ThemeInfo(
    ResourceDictionaryLocation.None,            //where theme specific resource dictionaries are located
                                                //(used if a resource is not found in the page,
                                                // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly   //where the generic resource dictionary is located
                                                //(used if a resource is not found in the page,
                                                // app, or any theme specific resource dictionaries)
)]

ファイル名:MainWindow.cs


using System.Windows;
using System.Windows.Controls;

namespace NoXAML34;

public class MainWindow : Window
{
    public MainWindow()
    {
        this.Title = "幅と高さの基本";
        this.Width = 640;
        this.Height = 400;

        var grid = new Grid
        {
            Margin = new Thickness(16)
        };
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) });
        grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
        grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
        Content = grid;

        // ① Autoサイズのボタン(内容に応じて縮む/伸びる)
        var autoButton = new Button
        {
            Content = "Auto(double.NaN) + Margin",
            Width = double.NaN,  // = Auto
            Height = double.NaN, // = Auto
            Margin = new Thickness(8),
            HorizontalAlignment = HorizontalAlignment.Left // 中身分だけ
        };
        Grid.SetRow(autoButton, 0);
        Grid.SetColumn(autoButton, 0);
        grid.Children.Add(autoButton);

        // ② Stretchで横に広げるテキストボックス
        var stretchBox = new TextBox
        {
            Text = "Stretchで列幅いっぱいに広がる(MinWidth=200)",
            Width = double.NaN, // Auto
            MinWidth = 200,
            Margin = new Thickness(8),
            HorizontalAlignment = HorizontalAlignment.Stretch
        };
        Grid.SetRow(stretchBox, 0);
        Grid.SetColumn(stretchBox, 1);
        grid.Children.Add(stretchBox);

        // ③ 下段に固定サイズのパネル
        var fixedPanel = new Border
        {
            BorderThickness = new Thickness(1),
            BorderBrush = SystemColors.ActiveBorderBrush,
            Width = 300,
            Height = 180,
            Margin = new Thickness(8),
            Child = new TextBlock { Text = "固定サイズ 300x180", VerticalAlignment = VerticalAlignment.Center, TextAlignment = TextAlignment.Center }
        };
        Grid.SetRow(fixedPanel, 1);
        Grid.SetColumnSpan(fixedPanel, 2);
        grid.Children.Add(fixedPanel);

        SizeChanged += (_, __) =>
        {
            Title = $"幅と高さの基本 - Actual: {ActualWidth:0} x {ActualHeight:0}";
        };
    }
}

コメント