Visibilityは、コントロールを表示、非表示に関わる列挙型です。3パターンあるので実際試してみます。
・初期状態は「Visible」でボタンが表示されています。
・「Hidden」ボタンを押すと、ボタンが非表示になりますが、領域はそのまま。
・「Collapsed」ボタンを押すと、ボタンが非表示になりかつ領域も削除されます。
ファイル名:NoXAML30.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 NoXAML30;
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 NoXAML30;
public class MainWindow : Window
{
public MainWindow()
{
this.Title = "Visibilityサンプル";
this.Width = 400;
this.Height = 300;
var panel = new StackPanel { Orientation = Orientation.Vertical, Margin = new Thickness(20) };
var targetButton = new Button { Content = "対象ボタン", Width = 120, Height = 40 };
panel.Children.Add(targetButton);
var visibleBtn = new Button { Content = "Visible" };
var hiddenBtn = new Button { Content = "Hidden" };
var collapsedBtn = new Button { Content = "Collapsed" };
visibleBtn.Click += (_, __) => targetButton.Visibility = Visibility.Visible;
hiddenBtn.Click += (_, __) => targetButton.Visibility = Visibility.Hidden;
collapsedBtn.Click += (_, __) => targetButton.Visibility = Visibility.Collapsed;
panel.Children.Add(visibleBtn);
panel.Children.Add(hiddenBtn);
panel.Children.Add(collapsedBtn);
this.Content = panel;
}
}
コメント