Quantcast
Viewing all articles
Browse latest Browse all 18858

Modify ItemsControl generated from XAML via Codebehind

I have a collection which I want to place on a grid. The collections items have properties that refer to where on the gird the item should be placed, and how many columns and rows the item should span. So the item looks sort of like this:

    public class Item
    {
        public int FromRow { get; set; }
        public int SpanRow { get; set; }
        public int FromColumn { get; set; }
        public int SpanColumn { get; set; }
    }



I want the grid to generate the right number of columns and rows dynamically for the collection, and display the collection trough data templates for each item in it. I believe I have solved this part of the problem like so:

<ItemsControl ItemTemplate="{StaticResource ItemTemplate}"
                  ItemsSource="{Binding ItemCollection"><ItemsControl.ItemsPanel><ItemsPanelTemplate><Grid x:Name="Grid" ShowGridLines="True" 
                    dataHelper:DynamicGrid.RowCount="7"
                    dataHelper:DynamicGrid.ColumnCount="{Binding ColumnCount}"
                    dataHelper:DynamicGrid.ColumnWidth="{Binding ColumnWidth}"></Grid></ItemsPanelTemplate></ItemsControl.ItemsPanel><ItemsControl.ItemContainerStyle><Style><Style.Setters><Setter Property="Grid.Row" Value="{Binding FromRow}" /><Setter Property="Grid.Column" Value="{Binding FromColumn}" /><Setter Property="Grid.RowSpan" Value="{Binding SpanRow}" /><Setter Property="Grid.ColumnSpan" Value="{Binding SpanColumn}" /></Style.Setters></Style></ItemsControl.ItemContainerStyle></ItemsControl>


I am using attached properties for the dynamic grid part. But there are two problems i cant figure out. 
  
First problem: I want to enable drag and drop on the items populating the grid so that I can manipulate the items position on the grid. Now I need the mouse position where the drop is supposed to accure. I searched around and found this:
private void OnMouseMove(object sender, MouseEventArgs e)
{        
  var element = (UIElement)e.Source;            
  int c = Grid.GetColumn(element);       
  int r = Grid.GetRow(element);    
}  


Which means that the grid must have UIElements in every cell, but the ItemsControl wont allow me to add anything to the grid which is understandable. It gives me: Error 5 Cannot explicitly modify Children collection of Panel used as ItemsPanel for ItemsControl. ItemsControl generates child elements for Panel.

I need to modify ItemsControl thats generated in XAML from codebehind. How can this be done?
  
Second problem: I want the rows and columns to have iterative headers. What I meen by that is that column one has '1' in its first cell, column two has '2' in its first cell, and so on. The same goes for rows. 

Since I can't include image of how it looks, here is a link: https://dl.dropboxusercontent.com/u/2849262/Skjermbilde.PNG

I hope I have explained my problems so you guys understand them. Any input would be much appreciated. And if you know a better way to do what I have explained, other tools than a normal Grid in a ItemsControl, please share! :D
  
Thanks in advance!

Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>