Enabling and disabling buttons in wpf

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.

Answers


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

Need Your Help

Dependency injection, polymorphism and OOP patterns (MVC, command and others)

java oop java-ee design-patterns dependency-injection

Intro: I work with Java EE 7 with glassfish 4 (so CDI container is weld). This is the third day I am reading about dependency injection and its principles, so if you find my question stupid just ex...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.