0

I am fairly new to both C# and WPF, but the project I am working on seems to be a great fit for them. I have a dynamic grid of labels set up based off of a database. I need to set them up for drag and drop. I have some labels set up as header, and they are populated based off the next seven fridays. I made a function to pull the data from the database and compare each date to the content of the header label content. Then I make labels containing a job number and state for each of the dates that match with the header.

Kind of like this on load
content = Job# + " " + JobState

-Date+7n----Date+7n----Date+7n----Date+7n----Date+7n----Date+7n-----Date+7n

Content------Content-----Content-----Content-----Content-----Content------Content

Content------Content-----Content-----Content-----Content-----Content------Content

Content------Content-----Content--------------------Content-----Content------Content

Content---------------------Content--------------------Content-----Content------Content

Content---------------------Content--------------------Content---------------------Content

Pretty much when the date runs out of jobs it just doesn't make anymore labels for that column.

I tried to make it as dynamic and expandable as possible so I have a forward and backwards button that push all the dates up a week or back a week then repopulates the grid based off of the new date at the top.

 private void AddLabel(ref int rowNum, string val, List<string>[] datGrid)
        {
            int rowLen;
            int margin1;
            int margin2;

            var lb = new Label();
            lb.HorizontalAlignment = HorizontalAlignment.Left;
            lb.VerticalAlignment = VerticalAlignment.Top;
            lb.Height = 32;
            lb.Width = 155;
            rowLen = datGrid[rowNum].Count;
            lb.AllowDrop = true;

            margin1 = 25 + (200 * (rowNum));
            margin2 = 10 + (40 * (rowLen));
            var gName = "grid" + (rowNum + 1) + (rowLen + 1);

            lb.Margin = new Thickness(margin1, margin2, 0, 0);
            lb.Content = val;
            lb.Name = gName;

            dyGrid.Children.Add(lb);
        }

Where I put the row number that the label needs to be added to as rowNum, the actual content of the label as val and datGrid as the array of lists that holds the names for all the labels. I just can't figure out how to add a mouseDown or other drag and drop events to the labels as I make it.

Community
  • 1
  • 1
Jeanze
  • 159
  • 1
  • 10

1 Answers1

1

The syntax you are looking for is

lb.MouseDown += new MouseButtonEventHandler(lb_MouseDown);

...

void lb_MouseDown(object sender, MouseButtonEventArgs e)
{
    // Handle MouseDown event here. 
    Label lb = sender as Label;
}

But that said, I would recommend rethinking your design.

From what I can guess from your code, it sounds like you have a Grid (which allows it's children to overlap), and you are placing all the labels in the first cell of the Grid, and using the Margin property to position them. Its not a very efficient design, and will likely cause you problems in the future.

I would instead switch to using an ItemsControl bound to your collection of items, and drawing it's ItemsPanelTemplate as a Grid and your ItemTemplate as the Label. Then you can create data items so they represent the actual data item being represented by that item. Check out this similar answer if you want an example for what I am talking about.

Also if you do this, look up Bea Stollnitz's code for dragging/dropping databound items. Or some variation of it... I'm sure there's a copy of the code somewhere in my answer history too if you have questions.

Community
  • 1
  • 1
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • Thank you! sorry I also forgot to add some context to some of the code I added. datGrid is a jagged array of lists. Each index in the array refers to a column, while I will only need to display 7 columns at a time I the number of rows to be infinitely expandable which I am still figuring out. On the function I put a foreach loop to clear out each list in the array, which seems like a bandaid fix for now. I will definitely do some research on ItemsControls and that dragging/dropping code though. Thank you very much for your response. – Jeanze Apr 26 '17 at 18:18