Change Template from Codebehind
-
Hi guys! Can anyone pleas help me with this:
<Grid ShowGridLines="True">
<ItemsControl Loaded="LoadedItemsControl"
ItemContainerStyleSelector="{StaticResource MElementStyleSelector}"
ItemsSource="{Binding Source={StaticResource MElements}}" x:Name="i1">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid ShowGridLines="True">//HERE SHOULD BE THE ROW AND COLUMNDEFINITIONS
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>As you can see in the code block I do have a ItemsTemplate with a Grid. What I need to do is to add Row- and Column definitions from Code behind, or if possible bind the needed amount in some way. The problem is, I have a huge number of cols and rows and I really do not want to write them line by line. In a normal Grid I would do it this way:
private void SetRowColDefinitions(int columns, int rows)
{
for (int i = 0; i < rows; i++)
{
RowDefinition row = new RowDefinition();
row.MinHeight = 50;
MGrid.RowDefinitions.Add(row);
}for (int i = 0; i < columns; i++) { ColumnDefinition col = new ColumnDefinition(); col.MinWidth = 50; MGrid.ColumnDefinitions.Add(col); } }
Unfortunately I cannot address the Grid in the Template. As always, help would be really appreciated!
-
Hi guys! Can anyone pleas help me with this:
<Grid ShowGridLines="True">
<ItemsControl Loaded="LoadedItemsControl"
ItemContainerStyleSelector="{StaticResource MElementStyleSelector}"
ItemsSource="{Binding Source={StaticResource MElements}}" x:Name="i1">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid ShowGridLines="True">//HERE SHOULD BE THE ROW AND COLUMNDEFINITIONS
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>As you can see in the code block I do have a ItemsTemplate with a Grid. What I need to do is to add Row- and Column definitions from Code behind, or if possible bind the needed amount in some way. The problem is, I have a huge number of cols and rows and I really do not want to write them line by line. In a normal Grid I would do it this way:
private void SetRowColDefinitions(int columns, int rows)
{
for (int i = 0; i < rows; i++)
{
RowDefinition row = new RowDefinition();
row.MinHeight = 50;
MGrid.RowDefinitions.Add(row);
}for (int i = 0; i < columns; i++) { ColumnDefinition col = new ColumnDefinition(); col.MinWidth = 50; MGrid.ColumnDefinitions.Add(col); } }
Unfortunately I cannot address the Grid in the Template. As always, help would be really appreciated!
You can use the Initialized or Loaded event on the Grid which is your ItemsPanel:
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid ShowGridLines="True"
Initialized="Grid_Initialized">
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>private void Grid_Initialized(object sender, EventArgs e)
{
var itemsGrid = (Grid)sender;AddColumn(itemsGrid);
AddColumn(itemsGrid);
AddColumn(itemsGrid);
}private static void AddColumn(Grid itemsGrid)
{
var column1 = new ColumnDefinition()
{
Width = new GridLength(100, GridUnitType.Pixel)
};
itemsGrid.ColumnDefinitions.Add(column1);
} -
You can use the Initialized or Loaded event on the Grid which is your ItemsPanel:
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid ShowGridLines="True"
Initialized="Grid_Initialized">
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>private void Grid_Initialized(object sender, EventArgs e)
{
var itemsGrid = (Grid)sender;AddColumn(itemsGrid);
AddColumn(itemsGrid);
AddColumn(itemsGrid);
}private static void AddColumn(Grid itemsGrid)
{
var column1 = new ColumnDefinition()
{
Width = new GridLength(100, GridUnitType.Pixel)
};
itemsGrid.ColumnDefinitions.Add(column1);
}