C# WPF仅禁用某一组按钮
如何禁用一组按钮,而不是全部按钮?我想禁用一组特定的按钮,比如说10个按钮,但仍然可以单击剩余的3个。C# WPF仅禁用某一组按钮,c#,wpf,C#,Wpf,如何禁用一组按钮,而不是全部按钮?我想禁用一组特定的按钮,比如说10个按钮,但仍然可以单击剩余的3个。 我想禁用的按钮将被简单地命名为button1、button2等等 您可以为此目的使用数据绑定: 正如您在下面的示例中所看到的,我创建了一个管理不同按钮组的小类: public partial class MainWindow : Window { ButtonGroupsController MyButtonGroups = new ButtonGroupsController();
我想禁用的按钮将被简单地命名为button1、button2等等 您可以为此目的使用数据绑定: 正如您在下面的示例中所看到的,我创建了一个管理不同按钮组的小类:
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>
点击我
点击我
点击我
点击我
点击我
点击我
祝你好运 为了模拟您的场景,我创建了一个包含十个按钮的列表,这些按钮的名称与您的按钮名称相似:
// 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);
}
要获取当前窗口的按钮,可以使用此小助手方法
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;
}
}
}
}
公共静态IEnumerable FindVisualChildren(DependencyObject depObj),其中T:DependencyObject
{
if(depObj!=null)
{
for(int i=0;i
用它就像
List<Button> buttons = FindVisualChildren<Button>(<< enter parent-object of the buttons here >>).ToList();
List buttons=FindVisualChildren(>).ToList();
如果父窗口是窗口,则可以使用:
List<Button> buttons = FindVisualChildren<Button>(this).ToList();
List<Button> buttons = FindVisualChildren<Button>(this.MyGrid).ToList();
List buttons=FindVisualChildren(this.ToList();
如果父网格的名称为“MyGrid”,则可以使用:
List<Button> buttons = FindVisualChildren<Button>(this).ToList();
List<Button> buttons = FindVisualChildren<Button>(this.MyGrid).ToList();
List buttons=FindVisualChildren(this.MyGrid.ToList();
将其IsEnabled
属性绑定到视图模型中的单个属性。@Jar.Jar您的意思是在发生某些事件时禁用按钮吗?@Nehorai是的,准确地说,我想制作一个按钮,单击该按钮时禁用所述组