locked
WPF Grid - Cell background color RRS feed

  • Question

  • Is it possible to obtain access to a cell on a Grid and change it's background color?

    Tuesday, October 31, 2006 3:04 PM

Answers

  • No.  The WPF Grid panel does have the concept of a "cell."  If you want a particular row/column intersection area (aka cell) to have a particular color, put some type of panel in that area and set its Background to that color.
    Tuesday, October 31, 2006 3:10 PM

All replies

  • No.  The WPF Grid panel does have the concept of a "cell."  If you want a particular row/column intersection area (aka cell) to have a particular color, put some type of panel in that area and set its Background to that color.
    Tuesday, October 31, 2006 3:10 PM
  • Ok. Thanks for the reply.
    Tuesday, October 31, 2006 7:16 PM
  • I can create a panel manualy, but can't to add it to "cell". Can you say, how to insert (assign) a panel into a "cell" ?
    Tuesday, January 22, 2008 1:14 AM
  • I have try something like this:
     DockPanel dp = new DockPanel();
    dp.Background= Brushes.CadetBlue;
    dp.Width=50;
    dp.Height=50;

    Grid g =new Grid();
    Grid.SetColumn(g,2);
    Grid.SetRow(g,2);
    grdMap.Children.Add(g);

    But don't see a new panel...
    • Proposed as answer by まるを Tuesday, December 17, 2013 6:06 AM
    Tuesday, January 22, 2008 1:23 AM
  • Try something like this:

     

    DockPanel dp = new DockPanel();
                    dp.Background= Brushes.CadetBlue;
                    dp.Width=50;
                    dp.Height=50;
                  
                    Grid g =new Grid();
                    Grid.RowDefinitions.Add(new RowDefinition()); //add one for each row

    Grod.RowDefinitions.Add(new RowDefinition());

    Grid.ColumnDefinitions.Add(new ColumnDefinition());  //add one for each column

    Grid.ColumnDefinitions.Add(new ColumnDefinition());

    Grid.ColumnDefinitions[0].Width = new GridLength(10, GridUnitType.Pixel); //define size for each column

    Grid.ColumnDefinitions[1].Width = new GridLength(1, GridUnitType.Star);

    Grid.RowDefinitions[0].Height = new GridLength(10, GridUnitType.Pixel); //define size for each row

    Grid.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Star);

    Grid.SetRow(dp,1); //set starting row 0  to n-1

    Grid.SetColumn(dp,1); //set starting column 0 to n-1

    Grid.SetColumnSpan(dp, 1); number of columns

    Grid.SetRowSpan(dp, 1); //number of rows
     grdMap.Children.Add(g);

     

    Tom

     

    Tuesday, January 22, 2008 2:06 AM
  • Design your elements like below:
    <Window x:Class="FirstWPFApplication.DridDemo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DridDemo" Height="150" Width="300">
        <Grid ShowGridLines="False" Name="MyGrid">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Name="Col0" Width="*" />
                <ColumnDefinition Name="Col1" Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Name="Row0" Height="Auto" />
                <RowDefinition Name="Row1" Height="Auto" />
                <RowDefinition Name="Row2" Height="Auto" />
            </Grid.RowDefinitions>
            <Label Name="lblName"  Grid.Row="0"  Grid.Column="0" Margin="5,5,5,5">
                Name:
            </Label>
            <DockPanel Name="NameDocP" Grid.Row="0"  Grid.Column="1" >
                <TextBox Name="txtName" Margin="5,5,5,5" GotFocus="txtPwd_GotFocus" LostFocus="txtName_LostFocus">
                    [Sample textbox]
                </TextBox>
            </DockPanel>       
            <Label Name="lblPassword" Grid.Row="1" Grid.Column="0" Margin="5,5,5,5" >
                Password:
            </Label>
            <DockPanel Name="PwdDocP" Grid.Row="1" Grid.Column="1" >
                <TextBox Name="txtPwd" Margin="5,5,5,5"
                     GotFocus="txtPwd_GotFocus" LostFocus="txtName_LostFocus">
                    [Sample textbox]
                </TextBox>
            </DockPanel>       
            <Button Name="cmdCancel" Width="45" Grid.Row="2" Grid.Column="1"
                    HorizontalAlignment="Left" Margin="10,10,0,0" >
                Cancel
            </Button>
            <Button Name="cmdOK" Width="45" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" Margin="0,10,10,0" >
                OK
            </Button>       
        </Grid>   
    </Window>


    In the code behing you can you some thing like below to change the color of the cell:
     private void txtPwd_GotFocus(object sender, RoutedEventArgs e)
            {
                if (sender == txtName)
                {
                    NameDocP.Background = Brushes.Yellow;
                    PwdDocP.Background = Brushes.White;
                }
                else
                {
                    NameDocP.Background = Brushes.White;
                    PwdDocP.Background = Brushes.Yellow;
                }
            }

            private void txtName_LostFocus(object sender, RoutedEventArgs e)
            {
                if (sender == txtName)
                {
                    NameDocP.Background = Brushes.White;
                }
                else
                {
                    PwdDocP.Background = Brushes.White;
                }
            }
    Thursday, August 27, 2009 6:54 AM
  • You can assign margins to place a panel or canvas wherever you like.
    Friday, August 3, 2018 12:16 PM