4

I have several Buttons on screen. On the click of one Button rest all other Buttons should be disabled. Is there a way to create one single method and set the enabling and disabling all Buttons instead of enabling and disabling individual Buttons.

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Virus
  • 3,215
  • 7
  • 29
  • 46

4 Answers4

4

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

<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;
        }
    }
}
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • This works if I am writing code under Button_Click event. But I am using MVVM pattern. So to implement this I need to know the sender. How do I know that? – Virus Jan 02 '13 at 04:30
  • I use commanding on button click and bind the parameters I need to pass. I recieve the parameters through the object but not the sender so I cannot disable rest of the buttons based on the sender. – Virus Jan 02 '13 at 04:40
  • If you are using MVVM light see this http://stackoverflow.com/questions/2963830/passing-event-args-and-sender-to-the-relaycommand. – Mark Hall Jan 02 '13 at 05:18
1

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.

Abdusalam Ben Haj
  • 5,343
  • 5
  • 31
  • 45
0

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.

Shahnawaz
  • 646
  • 1
  • 11
  • 25
0

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.

Handle PreviewMouseLeftButtonDown event in the Parent Window, instead:

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

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;
        }
    }
}
}
Community
  • 1
  • 1
Ramin
  • 2,133
  • 14
  • 22
  • The problem is the sender. As I am using MVVM pattern, how will I get to know the sender? – Virus Jan 02 '13 at 04:31