如何将按钮的单击事件设置为MainWindow中的功能?

时间:2015-10-29 18:51:30

标签: c# wpf

我有一个类从API中获取图像,对其进行操作,然后将其返回到按钮内。

我有另一个调用它的类,并将一组这些图像/按钮放入一个适当的Grid中,该Grid放在我的MainWindow中

我希望单击按钮折叠图像网格并显示具有放大图像的不同网格。我无法弄清楚如何附加Click虽然。

在我的图像课中,我有这个来设置图像

internal System.Windows.Controls.Button SetupImage()
{
    // Create and manipulate image
    System.Windows.Controls.Image _Image = new System.Windows.Controls.Image();
    this.CreateBitmapImage();
    Bitmap _Bitmap = this.CreateBitmap();
    if (_Bitmap.Width < _Bitmap.Height)
    {
        _Bitmap = this.RotateImage(_Bitmap, 90);
    }
    this.ConvertBitmap(_Bitmap);
    _Image.Source = this.BitmapImage;

    // Create a Button to put the Image in
    System.Windows.Controls.Button _ImageButton = new Button();
    _ImageButton.Style = (Style)Application.Current.FindResource("ButtonTransparent");
    _ImageButton.Content = _Image;

    // HOW DO I DO THIS BIT HERE? =========
    _ImageButton.Click += Application.Current.MainWindow.btnImage_Click;
    // ====================================

    return _ImageButton;
}

在MainWindow.xaml.cs中,我有一个点击应该调用的函数

/// <summary>
/// Get the image clicked on and show the zoomed image version
/// </summary>
private void btnImage_Click(object sender, RoutedEventArgs e)
{
    System.Windows.Controls.Image _ButtonImage = (System.Windows.Controls.Image)((Button)sender).Content;
    imgZoomImage.Source = image.Source;
    grdDetails.Visibility = Visibility.Collapsed;
    grdZoomImage.Visibility = Visibility.Visible;
}

编辑:

显示完整流程的更多代码。

在MainWindow.xaml.cs中构建网格

grdImages.Children.Clear();
if (_TrayImages != null && _TrayImages.Count > 0)
{
    Grid _ImageGrid = new Grid();
    _ImageGrid = this.ImageGridGenerator.Build(_TrayImages);
    grdImages.Children.Add(_ImageGrid);
}

ImageGridGenerator的代码如下

/// <summary>
/// Call a Grid Builder function appropriate for the number of images
/// </summary>
internal Grid Build(List<myImage> myImages)
{
    Grid _ImageGrid = new Grid();

    switch (myImages.Count())
    {
        case 1:
            this.BuildOneImageGrid(ref _ImageGrid, myImages[0]);
            break;
        case 2:
            this.BuildTwoImageGrid(ref _ImageGrid, myImages);
            break;
        case 3:
            this.BuildThreeImageGrid(ref _ImageGrid, myImages);
            break;
        default:
            // do the default action
            break;
    }

    return _ImageGrid;
}

/// <summary>
/// Create a Grid with one image
/// </summary>
private void BuildOneImageGrid(ref Grid imageGrid, MyImage myImage)
{
    System.Windows.Controls.Button _ImageButton = myImage.SetupImage();

    _ImageButton.HorizontalAlignment = HorizontalAlignment.Left;
    imageGrid.Children.Add(_ImageButton);
}

2 个答案:

答案 0 :(得分:0)

将EventHandler添加到您的图像类中,处理图像类中的单击并将参数传递给附加到EventHandler的任何人。

在您的图片类中:

public event EventHandler<RoutedEventArgs> ImageClicked;

private void OnClicked(object sender, RoutedEventArgs e)
{
    if (ImageClicked != null)
        ImageClicked(sender, e);
}

将OnClicked附加到Image类的Clicked事件。

现在,您可以将MainWindow.xaml.cs中的处理程序附加到Image事件中。

答案 1 :(得分:0)

根据@Kyhad的答案,我无法弄清楚如何让EventHandler附加

通过循环遍历我的ImageGridGenerator返回的网格中的所有内容,设置MainWindow.xaml.cs中的Clicks

Grid _ImageGrid = new Grid();
_ImageGrid = this.ImageGridGenerator.Build(Images);

// Attach click events
foreach (System.Windows.Controls.Button _ImageButton in _ImageGrid.Children)
{
    _ImageButton.Click += btnImage_Click;
}
grdImages.Children.Add(_ImageGrid);

这并不完美,但确实有效。