この記事では Button を活性/非活性にする方法について説明をします。
公式サイト情報はコチラを参照してください。
環境
開発環境 | Microsoft Visual Studio Enterprise 2019 Version 16.11.5 |
Framework | Microsoft .NET Framewohttps://blog.hiros-dot.net/?p=11007rk Version 4.8.04161 |
ボタンを活性/非活性にする
ボタンの活性/非活性は IsEnabled プロパティで設定します。
True で活性化、 False で非活性化にします。既定値は True です。
XAML の例
XAML でボタンを非活性にする例を以下に示します。
XAML の例
<Window x:Class="WinUISample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:WinUISample" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <StackPanel x:Name="myStackPanel" Orientation="Vertical" HorizontalAlignment="Left" > <!--<CheckBox x:Name="myCheckbox" Content="活性化" Checked="CheckBox_Checked" Unchecked="myCheckbox_Unchecked" />--> <Button x:Name="myButton" Content="非活性" IsEnabled="False"/> <!--<TextBlock x:Name="myTextBlock" />--> </StackPanel> </Window>
C# の例
以下は Checkbox のチェック状態に合わせて、ボタンの活性、非活性を切り替える例です。
<Window x:Class="WinUISample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:WinUISample" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <StackPanel x:Name="myStackPanel" Orientation="Vertical" HorizontalAlignment="Left" > <CheckBox x:Name="myCheckbox" Content="活性化" Checked="CheckBox_Checked" Unchecked="myCheckbox_Unchecked" /> <Button x:Name="myButton" Content="活性" IsEnabled="False"/> <TextBlock x:Name="myTextBlock" /> </StackPanel> </Window>
C# の例
private void CheckBox_Checked(object sender, RoutedEventArgs e) { myButton.IsEnabled = true; myButton.Content = "活性"; } private void myCheckbox_Unchecked(object sender, RoutedEventArgs e) { myButton.IsEnabled = false; myButton.Content = "非活性"; }
WinUi Tips
本サイトでまとめている WinUI Tips の一覧はこちらから確認できます。
Please follow and like us:
コメント