locked
XAML. C++. Get Children Of A Grid Column?

    Question

  • Sorry, but I am a complete newbee here.

    I have XAML that contains a Grid with one row and three columns.

    In code: How can I empty the contents of a specific Grid column (the Children of the Grid specific Column)? Is this possible? I need to empty it (not 'hide' the contents) because I want to replace the Children of the column with new Child's.

    I cannot even find out how to obtain the Children of a specific Grid column.

    Thanks

    Wednesday, April 10, 2013 6:17 PM

Answers

  • The following code will examine the Children of a Grid and remove items that are in a specified column. I also included code for how you would place a new item (I used a TextBlock for simplicity) at a specific location in the Grid.

    	// The column we want to clear from.
    	int columnToClear = 0;
    
    	// This assumes that the Grid control has an x:Name="SomeGrid" attribute. Change SomeGrid to the name of
    	// your Grid control.
    	for (auto i = 0U; i < SomeGrid->Children->Size; /* Do nothing; we'll be handling incrementing below. */)
    	{
    		auto child = SomeGrid->Children->GetAt(i);
    		auto columnOfChild = SomeGrid->GetColumn(safe_cast<Windows::UI::Xaml::FrameworkElement^>(child));
    		if (columnOfChild == columnToClear)
    		{
    			SomeGrid->Children->RemoveAt(i);
    		}
    		else
    		{
    			// Only increment if we didn't remove something, otherwise we'd wind up skipping any element
    			// that came after an element we removed.
    			++i;
    		}
    	}
    
    	// This is an example of how to add an element to a Grid at a specific row and column. Since
    	// the row and column are attached properties that come from Grid, we need to use the SetValue
    	// member function.
    	auto textBlock = ref new Windows::UI::Xaml::Controls::TextBlock();
    	textBlock->Text = "Hello C++!";
    	// Add it to the columnToClear column. We need to create a boxed int here since SetValue is
    	// expecting a Platform::Object derived item (such that we can't just outright specify the int value).
    	auto boxedColumn = ref new Platform::Box<int>(columnToClear);
    	textBlock->SetValue(Windows::UI::Xaml::Controls::Grid::ColumnProperty, boxedColumn);
    	// Add it to row 1 (i.e. the second row).
    	auto boxedRow = ref new Platform::Box<int>(1);
    	textBlock->SetValue(Windows::UI::Xaml::Controls::Grid::RowProperty, boxedRow);
    	textBlock->FontSize = 26.6667;
    	// Add the TextBlock to the Grid's children.
    	SomeGrid->Children->Append(textBlock);
    


    Visual C++ MVP | Website | Blog | @mikebmcl | Windows Store DirectX Game Template

    • Proposed as answer by MikeBMcLMVP Wednesday, April 10, 2013 9:49 PM
    • Marked as answer by RSullivan59 Wednesday, April 10, 2013 10:43 PM
    Wednesday, April 10, 2013 9:49 PM

All replies

  • The following code will examine the Children of a Grid and remove items that are in a specified column. I also included code for how you would place a new item (I used a TextBlock for simplicity) at a specific location in the Grid.

    	// The column we want to clear from.
    	int columnToClear = 0;
    
    	// This assumes that the Grid control has an x:Name="SomeGrid" attribute. Change SomeGrid to the name of
    	// your Grid control.
    	for (auto i = 0U; i < SomeGrid->Children->Size; /* Do nothing; we'll be handling incrementing below. */)
    	{
    		auto child = SomeGrid->Children->GetAt(i);
    		auto columnOfChild = SomeGrid->GetColumn(safe_cast<Windows::UI::Xaml::FrameworkElement^>(child));
    		if (columnOfChild == columnToClear)
    		{
    			SomeGrid->Children->RemoveAt(i);
    		}
    		else
    		{
    			// Only increment if we didn't remove something, otherwise we'd wind up skipping any element
    			// that came after an element we removed.
    			++i;
    		}
    	}
    
    	// This is an example of how to add an element to a Grid at a specific row and column. Since
    	// the row and column are attached properties that come from Grid, we need to use the SetValue
    	// member function.
    	auto textBlock = ref new Windows::UI::Xaml::Controls::TextBlock();
    	textBlock->Text = "Hello C++!";
    	// Add it to the columnToClear column. We need to create a boxed int here since SetValue is
    	// expecting a Platform::Object derived item (such that we can't just outright specify the int value).
    	auto boxedColumn = ref new Platform::Box<int>(columnToClear);
    	textBlock->SetValue(Windows::UI::Xaml::Controls::Grid::ColumnProperty, boxedColumn);
    	// Add it to row 1 (i.e. the second row).
    	auto boxedRow = ref new Platform::Box<int>(1);
    	textBlock->SetValue(Windows::UI::Xaml::Controls::Grid::RowProperty, boxedRow);
    	textBlock->FontSize = 26.6667;
    	// Add the TextBlock to the Grid's children.
    	SomeGrid->Children->Append(textBlock);
    


    Visual C++ MVP | Website | Blog | @mikebmcl | Windows Store DirectX Game Template

    • Proposed as answer by MikeBMcLMVP Wednesday, April 10, 2013 9:49 PM
    • Marked as answer by RSullivan59 Wednesday, April 10, 2013 10:43 PM
    Wednesday, April 10, 2013 9:49 PM
  • Thanks Mike! You are my Hero!

    BTW:

    int columnToClear = 0;

    if(SomeGrid->Children->Size) // It Could Be Empty
    {
     for (auto i = 0U; i < SomeGrid->Children-Size;i++)
     {
      auto child = SomeGrid->Children->GetAt(i);
      auto columnOfChild = SomeGrid->GetColumn  (safe_cast<Windows::UI::Xaml::FrameworkElement^>(child));
      if (columnOfChild == columnToClear)
      {
       SomeGrid->Children->RemoveAt(i--); // I Have A Horrible Aversion To else's
       }
      }
     }

    Thanks again, you certainly saved me a whole lot of time!

    Wednesday, April 10, 2013 10:42 PM
  • While your i-- code will work, it's quite fragile given the general lack of knowledge among many (most?) developers of the difference between i-- and --i.

    If anyone else has to maintain it, especially if they're an older programmer (or a younger one who's been taught the now mostly meaningless because optimizing compilers deal with it properly platitude that one should always use --i to eliminate temp variables), they may eventually swap in --i and break the code without realizing what happened and why.

    If you're the only one who will ever be dealing with the code then it's less dangerous. Regardless, I recommend trying to get over the aversion to else statements since you are likely to wind up with very convoluted, fragile, unmaintainable code just to avoid an ordinary, non-dangerous programming construct. Clean, simple, readable code is a good thing.

    Anyway, glad I could help.


    Visual C++ MVP | Website | Blog | @mikebmcl | Windows Store DirectX Game Template

    Wednesday, April 10, 2013 11:49 PM