2016년 10월 17일 월요일

2016년 10월 11일 화요일

2016년 10월 5일 수요일

c# wpf Create Random Number of Grid and Image Load Drop in grid cell

Main code

-------------------------------------------------------
    public partial class MainWindow : Window
    {
        List EvtList;
        public MainWindow()
        {
            EvtList = new List();
            InitializeComponent();

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                   
                    StackPanel stp = new StackPanel();
                    stp.Width = 80;
                    stp.Height = 80;
                    stp.AllowDrop = true;
                    stp.Background = Brushes.Transparent; // Here is very Important
                    Grid.SetColumn(stp, j);
                    Grid.SetRow(stp, i);

                    Image imgbox = new Image();
                    imgbox.Width = 80;
                    imgbox.Height = 80;
                    stp.Children.Add(imgbox);

                    EventClass evc = new EventClass(stp, lbl,imgbox, i, j);
                    EvtList.Add(evc);
                    stp.Drop += evc.DropEventMethod;

                    gridmain.Children.Add(stp);

                }
            }


        }
-------------------------------------------------------



Class Code
-------------------------------------------------------
    public class EventClass
    {
        public StackPanel Btn;
        public Label Lbl;
        public Image Img;
        public int Rownum;
        public int Colnum;

        public EventClass(StackPanel btn, Label lbl , Image img,int row,int col)
        {
            Btn = btn;
            Rownum = row;
            Colnum = col;
            Lbl = lbl;
            Img = img;
        }

        public void DropEventMethod(object ss, DragEventArgs ee)
        {
            this.Lbl.Dispatcher.BeginInvoke((Action)(() => Lbl.Content =  $"Click i = {this.Rownum}  j = {this.Colnum}"  ));
            string[] files = (string[])ee.Data.GetData(DataFormats.FileDrop);
            Lbl.Content = files[0];
            Img.Source = new BitmapImage(new Uri(files[0]));
        }
       

    }
-------------------------------------------------------


Xaml Code
-------------------------------------------------------
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
   
       
           
           
           
           
       

       
           
           
           
           
       
       
   


-------------------------------------------------------

c# WPF Create Event and Subscribe with for implement

First, Create Event Class

---------------------------------------
public class EventClass
    {
        public Button Btn;
        public Label Lbl;
        public int Rownum;
        public int Colnum;

        public EventClass(Button btn, Label lbl ,int row,int col)
        {
            Btn = btn;
            Rownum = row;
            Colnum = col;
            Lbl = lbl;
        }

        public void ClickEventMethod(object ss, RoutedEventArgs ee)
        {
            this.Lbl.Dispatcher.BeginInvoke((Action)(() => Lbl.Content =  {"Click i = {this.Rownum}  j = {this.Colnum}"  ));
            Console.WriteLine($"Click i = {this.Rownum}  j = {this.Colnum}");
        }


    }

---------------------------------------



Here is main Code

-------------------------------------
public partial class MainWindow : Window
    {
        List EvtList;
        public MainWindow()
        {
            EvtList = new List();
            InitializeComponent();

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                   
                    Button btn = new Button();
                    btn.Width = 80;
                    btn.Height = 80;
                    Grid.SetColumn(btn, j);
                    Grid.SetRow(btn, i);


                    EventClass evc = new EventClass(btn, lbl, i, j);
                    EvtList.Add(evc);
                    btn.Click += evc.ClickEventMethod;

                    gridmain.Children.Add(btn);

                }
            }
        }
    }

-------------------------------------



Here is Xaml

-------------------------------------
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
   
       
           
           
           
           
       
       
           
           
           
           
       
       
   

-------------------------------------