WPF仅禁用特定的按钮组

时间:2018-07-28 10:17:52

标签: c# wpf

如何禁用一组按钮,但不能全部禁用?我想禁用某组(例如10个按钮),但仍然可以单击其余3个按钮。 我要禁用的按钮将被简单地命名为button1,button2等。

2 个答案:

答案 0 :(得分:0)

您可以为此目的使用数据绑定:

如下面的示例所示,我创建了一个小类来管理不同的按钮组:

 public partial class MainWindow : Window
{
    ButtonGroupsController MyButtonGroups = new ButtonGroupsController();
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = MyButtonGroups;
        MyButtonGroups.Group1Enable = false;
        MyButtonGroups.Group2Enable = true;

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MyButtonGroups.Group1Enable = !MyButtonGroups.Group1Enable;
        MyButtonGroups.Group2Enable = !MyButtonGroups.Group2Enable;
    }
}

public class ButtonGroupsController : INotifyPropertyChanged
{
    private bool group1Enable = false;
    private bool group2Enable = false;

    public bool Group1Enable
    {
        get
        {
            return group1Enable;
        }
        set
        {
            group1Enable = value;
            if (PropertyChanged!=null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Group1Enable"));
            }
        }
    }
    public bool Group2Enable
    {
        get
        {
            return group2Enable;
        }
        set
        {
            group2Enable = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Group2Enable"));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

和XAML:

<StackPanel>
        <Button  IsEnabled="{Binding Group1Enable , Mode=OneWay}" Click="Button_Click">click me</Button>
        <Button IsEnabled="{Binding Group1Enable, Mode=OneWay}" Click="Button_Click">click me</Button>
        <Button IsEnabled="{Binding Group1Enable, Mode=OneWay}" Click="Button_Click">click me</Button>
        <Button  IsEnabled="{Binding Group2Enable, Mode=OneWay }" Click="Button_Click">click me</Button>
        <Button IsEnabled="{Binding Group2Enable, Mode=OneWay}" Click="Button_Click">click me</Button>
        <Button IsEnabled="{Binding Group2Enable, Mode=OneWay}" Click="Button_Click">click me</Button>
    </StackPanel>

祝你好运!

答案 1 :(得分:0)

为模拟您的情况,我创建了一个包含十个按钮的列表,所有按钮的名称都与按钮的名称相似:

// create some buttons for simulation purposes, give them a name and put them into a list
var buttons = new List<Button>();    
for (int i = 0; i < 10; ++i)
{
    var button = new Button();
    button.Name = "button" + i;
    buttons.Add(button);
}

要禁用按钮,可以使用Clemens在上面的评论中提到的IsEnabled属性:

// option A: disable all buttons except button at index 2 
for (int i = 0; i < buttons.Count; ++i)
{
    if (i == 2)
    {
        continue;
    }

    buttons[i].IsEnabled = false;
}


// option b: disable all buttons except button with name 'button3'
foreach (var button in buttons)
{
    if (button.Name == "button3")
    {
        continue;
    }

    button.IsEnabled = false;
}

要获取当前窗口的按钮,可以使用此小帮手方法...

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;
            }
        }
    }
}

并像...一样使用它

List<Button> buttons = FindVisualChildren<Button>(<< enter parent-object of the buttons here >>).ToList();

如果父窗口是窗口,则可以使用:

List<Button> buttons = FindVisualChildren<Button>(this).ToList();

如果父级是名称为“ MyGrid”的网格,则可以使用:

List<Button> buttons = FindVisualChildren<Button>(this.MyGrid).ToList();