Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Change Template from Codebehind

Change Template from Codebehind

Scheduled Pinned Locked Moved WCF and WF
helpcsswpfwcf
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ezazazel
    wrote on last edited by
    #1

    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!

    H 1 Reply Last reply
    0
    • E ezazazel

      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!

      H Offline
      H Offline
      Hauke Wittern
      wrote on last edited by
      #2

      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);
      }

      E 1 Reply Last reply
      0
      • H Hauke Wittern

        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);
        }

        E Offline
        E Offline
        ezazazel
        wrote on last edited by
        #3

        Thank you very much! Helps me a lot!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups