在 wpf 中启用和禁用按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14108949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:51:01  来源:igfitidea点击:

Enabling and disabling buttons in wpf

wpfbuttonmouseevent

提问by Virus

I have several Buttonson screen. On the click of one Buttonrest all other Buttonsshould be disabled. Is there a way to create one single method and set the enabling and disabling all Buttonsinstead of enabling and disabling individual Buttons.

我有几个Buttons在屏幕上。单击一个Button休息,所有其他Buttons都应该被禁用。有没有办法创建一个方法并设置启用和禁用 allButtons而不是启用和禁用个体Buttons.

回答by Mark Hall

This method assumes that the Buttons are on the same Grid, it reverses the Enabled state of every button except of the one doing the clicking.

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

MainWindow.xaml

主窗口.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

主窗口.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;
        }
    }
}

回答by Abdusalam Ben Haj

Write one method that handles all your buttons click. here is an example:

编写一种处理所有按钮单击的方法。这是一个例子:

Private Sub ButtonsClick(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles button1.Click ,button2.Click ,button3.Click

button1.IsEnabled = (button1 is sender)
button2.IsEnabled = (button2 is sender)
button3.IsEnabled = (button3 is sender)


End Sub

EDIT: I overlooked the fact that you don't want to enable/disable individual buttons. So I would recommend Mark answer.

编辑:我忽略了您不想启用/禁用单个按钮的事实。所以我会推荐马克回答。

回答by Ramin

I complete Mark Hall answer with the linked provided by Christian in his comment. You might like it.
There is no need that Buttons be in one Grid or handling all their click events.

我用 Christian 在评论中提供的链接完成了 Mark Hall 的回答。你可能会喜欢。
按钮不需要在一个网格中或处理它们的所有点击事件。

Handle PreviewMouseLeftButtonDownevent in the Parent Window, instead:

PreviewMouseLeftButtonDown在父窗口中处理事件,而不是:

void window_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e)
{
Window win = sender as Window;
if (e.OriginalSource is Button)
{
     Button btn=(Button)e.Source;
     foreach(Button button0 in FindVisualChildren<Button>(win))
     {
        if (btn != button0)
            button0.IsEnable = false;
     }
}
}

It uses the following method. see Find all controls in WPF Window by type

它使用以下方法。请参阅按类型查找 WPF 窗口中的所有控件

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
        if (child != null && child is T)
        {
            yield return (T)child;
        }

        foreach (T childOfChild in FindVisualChildren<T>(child))
        {
            yield return childOfChild;
        }
    }
}
}

回答by Shahnawaz

I have an idea for you, create a method that disables all buttons first. Call that method first whenever clicking any button so all the buttons are disabled, then just enable the button you just clicked for that individual button click event handler method.

我给你一个主意,先创建一个禁用所有按钮的方法。每当单击任何按钮时首先调用该方法,以便禁用所有按钮,然后只需为该单个按钮单击事件处理程序方法启用您刚刚单击的按钮。