在wpf中启用和禁用按钮

时间:2013-01-01 08:15:25

标签: wpf button mouseevent

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

4 个答案:

答案 0 :(得分:4)

此方法假设按钮位于同一个网格上,它会反转每个按钮的启用状态,但执行单击的按钮除外。

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

答案 1 :(得分:1)

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

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
编辑:我忽略了你不想启用/禁用单个按钮的事实。所以我建议Mark回答。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

我完成马克·霍尔的答案,并附上克里斯蒂安在评论中提供的链接。你可能会喜欢它 按钮不需要在一个网格中或处理所有点击事件。

在父窗口中处理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;
     }
}
}

它使用以下方法。见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;
        }
    }
}
}