如何从具有不同参数的编程创建按钮调用方法?

时间:2014-11-20 14:56:36

标签: c# wpf methods click raise

我的问题是如何使用按钮特定参数从另一个对象调用现有方法。 这是我需要调用的方法(来自MainWindow):

partial class Sidebar : Window
{
    [...]
    internal void SetPosition(System.Drawing.Rectangle workingarea, bool left)
    {
      Overlay.Properties.Settings.Default.SidebarSide = left;
      Overlay.Properties.Settings.Default.Top = this.Top = workspace.Top;
      Overlay.Properties.Settings.Default.Left = this.Left = workspace.Left;
      Overlay.Properties.Settings.Default.Save();
      this.Height = workspace.Height;
      this.Width = workspace.Width;
      timeGrid.Style = gridStyle;
      Refresh();
    }
    [...]
}

以下是为连接到机器的每个屏幕创建按钮(以及更多)的方法

class SettingsWindow : Window
{
    [...]
    private void SidebarTab_Initialized(object sender, EventArgs e)
            {
                Canvas monitorCanvas = new Canvas();
                spPosition.Children.Add(monitorCanvas);

            System.Windows.Forms.Screen currentScreen = System.Windows.Forms.Screen.FromHandle(
                new System.Windows.Interop.WindowInteropHelper(this).Handle);
            System.Windows.Forms.Screen[] screens = System.Windows.Forms.Screen.AllScreens;

            Point min = new Point(0,0);
            Point max = new Point(0,0);

            for (int i = 0; i < screens.Length; i++)
            {
                min.X = min.X < screens[i].Bounds.X ? min.X : screens[i].Bounds.X;
                min.Y = min.Y < screens[i].Bounds.Y ? min.Y : screens[i].Bounds.Y;

                max.X = max.X > (screens[i].Bounds.X + screens[i].Bounds.Width) ? max.X : (screens[i].Bounds.X + screens[i].Bounds.Width);
                max.Y = max.Y > (screens[i].Bounds.Y + screens[i].Bounds.Height) ? max.Y : (screens[i].Bounds.Y + screens[i].Bounds.Height);
            }
            [...]


            for (int i = 0; i < screens.Length; i++)
            {

                Border monitor = new Border();
                monitor.BorderBrush = Brushes.Black;
                monitor.BorderThickness = new Thickness(1);
                Canvas.SetTop(monitor, (screens[i].Bounds.Top - min.Y) / scale);
                Canvas.SetLeft(monitor, (screens[i].Bounds.Left - min.X) / scale);
                monitor.Width = screens[i].Bounds.Width / scale;
                monitor.Height = screens[i].Bounds.Height / scale;

                DockPanel dp = new DockPanel();

                Button monLeft = new Button();
                monLeft.Width = scale;
                DockPanel.SetDock(monLeft, Dock.Left);

                Button monRight = new Button();
                monRight.Width = scale;
                DockPanel.SetDock(monRight, Dock.Right);
                [...]
                }
            }

}

如您所见,我需要为机器上的每个屏幕设置两个按钮。
monLeft.Click = SetPosition(screens[i].WorkingArea, true);
我需要monRight.Click = SetPosition(screens[i].WorkingArea, true);

提前致谢。

2 个答案:

答案 0 :(得分:1)

使用lambda定义事件处理程序。这允许您关闭处理程序中您需要的局部变量。

请注意,闭包会关闭变量,而不是值,因此您不希望关闭i(它不会成为事件发生时您想要的值火灾)。您需要在循环内部复制循环变量,以便您可以关闭 。那么,或者使用foreach循环(在C#5.0 +中)而不是for循环。

foreach(var screen in screens)
{
    //...

    Button monRight = new Button();
    monRight.Width = scale;
    DockPanel.SetDock(monRight, Dock.Right);
    monRight.Click += (s,e) => SetPosition(screen.WorkingArea, true);
}

答案 1 :(得分:0)

嗯,正如您在http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.click(v=vs.110).aspx中看到的那样,Button.Click是一个事件,因此您需要为其分配一个事件处理程序:

您可以将SetPosition()方法包装在事件处理程序中,并将Button.Click事件的事件处理程序分配给它,或者您可以通过lambda构造执行此操作,如上一个答案所示。

另一种方法是为您的按钮设置Button.Command,方法是实施一个ICommand实例,调用SetPosition()方法。