gpt4 book ai didi

wpf - 在 wpf 中启用和禁用按钮

转载 作者:行者123 更新时间:2023-12-01 11:48:04 25 4
gpt4 key购买 nike

我有好几个 Buttons在屏幕上。一键点击 Button其余所有 Buttons应该禁用。有没有办法创建一个方法并设置所有启用和禁用Buttons而不是启用和禁用个人 Buttons .

最佳答案

此方法假定按钮位于同一个网格上,它反转每个按钮的启用状态,除了点击的按钮。

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button_Click" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,43,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button_Click" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,76,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="button_Click" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,109,0,0" Name="button4" VerticalAlignment="Top" Width="75" Click="button_Click" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,142,0,0" Name="button5" VerticalAlignment="Top" Width="75" Click="button_Click" />
</Grid>
</Window>

MainWindow.xaml.cs
private void button_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
foreach (FrameworkElement item in ((Panel)btn.Parent).Children )
{
if (item is Button)
{
if (btn.Name != item.Name)
item.IsEnabled = !item.IsEnabled;
}
}
}

关于wpf - 在 wpf 中启用和禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14108949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com