1

I have created this grid :

Grid grid = new Grid // new Grid
{
    Width = 1500,
    Height = 1500,
    HorizontalAlignment = HorizontalAlignment.Left,
    VerticalAlignment = VerticalAlignment.Center,
    Background = new SolidColorBrush(Colors.Coral)
};

var columnDefinition = new ColumnDefinition { Width = new GridLength(200) }; // column
var columnDefinition1 = new ColumnDefinition { Width = new GridLength(200) };
var columnDefinition2 = new ColumnDefinition { Width = new GridLength(200) };
var columnDefinition3 = new ColumnDefinition { Width = new GridLength(200) };

grid.ColumnDefinitions.Add(columnDefinition); // add column to grid
grid.ColumnDefinitions.Add(columnDefinition1);
grid.ColumnDefinitions.Add(columnDefinition2);
grid.ColumnDefinitions.Add(columnDefinition3);

var rowDefinition = new RowDefinition { Height = new GridLength(300) }; // row
var rowDefinition1 = new RowDefinition { Height = new GridLength(300) };
var rowDefinition2 = new RowDefinition { Height = new GridLength(300) };
var rowDefinition3 = new RowDefinition { Height = new GridLength(300) };

grid.RowDefinitions.Add(rowDefinition); // add row to grid
grid.RowDefinitions.Add(rowDefinition1);
grid.RowDefinitions.Add(rowDefinition2);
grid.RowDefinitions.Add(rowDefinition3);

var textBlock1 = new TextBlock // new textBlock
{
    Text = "TextBox 1 ",
    FontSize = 20,
    Foreground = new SolidColorBrush(Colors.White),
    Width = 100
};

Grid.SetColumn(textBlock1, 0); 
Grid.SetRow(textBlock1, 0); 

var rec = new Rectangle(); // new Rectangle
{
    Width = 70;
    Height = 70;

    rec.Fill = linearGradientBrush;
}

Grid.SetColumn(rec, 1);
Grid.SetRow(rec, 0);
grid.Children.Add(textBlock1); 
grid.Children.Add(rec); 
MainGrid.Children.Add(grid); // Add grid to main grid

But all I can see is small rectangle of coral color (which is color of my grid) in the middle of the screen. I know it's easier to create grid using XAML, I just want to know how to do it in C#. Thanks for help

Peter F
  • 83
  • 2
  • 3
  • 12
  • creating UI elements in procedural code in XAML-based technologies is discouraged, cumbersome, probably a bad idea, and just plain wrong. There is no logical reason to do this. – Federico Berasategui Mar 18 '14 at 14:35
  • @HighCore Thanks. Do you have some useful link where I could read about this? – Peter F Mar 18 '14 at 15:31
  • @HighCore: what if you don't know what UI elements are needed until runtime, e.g., [this question](http://stackoverflow.com/q/26856909/886887)? – Harry Johnston Nov 11 '14 at 22:14
  • @HarryJohnston `I want there to be a certain number of...` - That's an `ItemsControl`. If you come up with a real scenario of what you need I can tell you how to do it properly. I have not, to this day, with 4+ years of WPF development under my belt and dozens of UIs and custom controls and whatnot, stumbled upon a SINGLE scenario where I was forced to create UI elements in procedural code. – Federico Berasategui Nov 11 '14 at 22:22

2 Answers2

0

Try using

rec.SetValue(Grid.RowProperty, 0);
rec.SetValue(Grid.ColumnProperty, 1);

Similarly for your textblock..

bit
  • 4,407
  • 1
  • 28
  • 50
0

The most obvious problem I see in your code is that you add the Grid children after you try to set the attached property. I believe the element needs to already be a child of the Grid object before you set the attached property. Otherwise, how would the static method know which Grid object's attached property should be set?

This code, an event handler subscribed to the Grid's Loaded event, works fine for me:

private void Grid_Loaded(object sender, RoutedEventArgs e)
{
    int rows = 2, columns = 3;

    for (int i = 0; i < rows; i++)
    {
        RowDefinition definition = new RowDefinition();

        definition.Height = new GridLength(1, GridUnitType.Star);
        grid.RowDefinitions.Add(definition);
    }

    for (int i = 0; i < columns; i++)
    {
        ColumnDefinition definition = new ColumnDefinition();

        definition.Width = new GridLength(1, GridUnitType.Star);
        grid.ColumnDefinitions.Add(definition);
    }

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < columns; j++)
        {
            TextBlock text = new TextBlock();

            text.Text = string.Format("column: {0}, row: {1}", j, i);
            text.FontSize = 36;
            text.HorizontalAlignment = HorizontalAlignment.Center;
            text.VerticalAlignment = VerticalAlignment.Center;
            text.Margin = new Thickness(10, 10, 10, 10);
            grid.Children.Add(text);

            Grid.SetRow(text, i);
            Grid.SetColumn(text, j);
        }
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • -1 your answer goes against all well-known, established practices and patterns in XAML based technologies. Also, all your horrible code can be replaced by a couple of lines of proper XAML. – Federico Berasategui Nov 11 '14 at 22:29
  • @ HighCore well it might be against best practices but I just wanted to know how to do it :). – Peter F Nov 11 '14 at 22:37
  • 1
    @HighCore: thanks for the editorial. You seem to be one of those dogmatic folks who can't stand the idea of procedural leaking into their declarative. Still, I notice you haven't bothered to actually _post_ an answer. So your claim that it's possible to address the OP's question in a strictly declarative way remains unsubstantiated. If you want to share knowledge that's great, but if you're just going to insult other people, I'm not sure why you are here. – Peter Duniho Nov 11 '14 at 22:43
  • @PeterDuniho LOL you have to be kidding. I can give you countless examples of answers I've posted in the past explaining how to do this **properly** in WPF. [1](http://stackoverflow.com/questions/15344022/how-can-i-get-the-position-of-textbox-that-has-been-pressed) [2](http://stackoverflow.com/a/21004225/643085) [3](http://stackoverflow.com/a/16227842/643085) I can find more if you want. I didn't post here because it would be *yet another* "delete all your code and start all over" type of answer, which I sincerely don't have time for right now. – Federico Berasategui Nov 11 '14 at 22:48