question

JohnOstler-6145 avatar image
0 Votes"
JohnOstler-6145 asked DaisyTian-1203 commented

problem with grid columns

Hello. I am creating a grid like this, i have a grid with two columns i want one on the right side to be 60 pixels no percentage and i want the column on the left to be maximum it can be minus the 60 pixel column on the right how do I do this thanks.


 // Create the Grid    
         DynamicGrid.Width = this.Width; 
         DynamicGrid.HorizontalAlignment = HorizontalAlignment.Left;
         DynamicGrid.VerticalAlignment = VerticalAlignment.Top;

         DynamicGrid.ShowGridLines = true;
         DynamicGrid.Background = new SolidColorBrush(Colors.LightSteelBlue);

         // Create Columns    
         ColumnDefinition gridCol1 = new ColumnDefinition();
         ColumnDefinition gridCol2 = new ColumnDefinition();

         gridCol1.Width = new GridLength(100, GridUnitType.Star);
         gridCol2.Width = new GridLength(60);
windows-wpf
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JohnOstler-6145 , what is your current problem for the code? Based on my test, the right side is set to be 60 pixels. However, I am not full understand the column on the left can be minus 60 pixels. Do you mean you need to change the left width manually in the runtime?

0 Votes 0 ·

Thanks for the response. I meant. I have a left column and a right column. During runtime people can change the size of the application and both columns will stretch but I only want the left one to stretch and I want the right one to stay at a constant 60. I also need the left column to always be 100% of what it can be. With the code shown above gridCol2 isn't visible. thanks

0 Votes 0 ·

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered DaisyTian-1203 commented

The code snippet:

 ColumnDefinition gridCol1 = new ColumnDefinition();
 ColumnDefinition gridCol2 = new ColumnDefinition();
    
 gridCol1.Width = new GridLength(1, GridUnitType.Star);
 gridCol2.Width = new GridLength(60);

has the same effect as below code section

  <Grid.ColumnDefinitions>
         <ColumnDefinition Width="1*"></ColumnDefinition>
         <ColumnDefinition Width="60"></ColumnDefinition>
  </Grid.ColumnDefinitions>   

I made the following additions to your code and tested it.

   public void InitGrid()
         {
             Grid DynamicGrid = new Grid();
             DynamicGrid.HorizontalAlignment = HorizontalAlignment.Left;
             DynamicGrid.VerticalAlignment = VerticalAlignment.Top;
             DynamicGrid.ShowGridLines = true;
             DynamicGrid.Background = new SolidColorBrush(Colors.LightSteelBlue);
             mySp.Children.Add(DynamicGrid);
    
             BindingOperations.SetBinding(DynamicGrid, WidthProperty, new Binding()
             {
                 RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(StackPanel), 1),
                 Path = new PropertyPath("Width"),
                 Mode = BindingMode.TwoWay
             }
             );
    
             ColumnDefinition gridCol1 = new ColumnDefinition();
             ColumnDefinition gridCol2 = new ColumnDefinition();
             DynamicGrid.ColumnDefinitions.Add(gridCol1);
             DynamicGrid.ColumnDefinitions.Add(gridCol2);
    
             gridCol1.Width = new GridLength(1, GridUnitType.Star);
             gridCol2.Width = new GridLength(60);
    
             TextBlock txt1 = new TextBlock();
             txt1.Text = "Colm 1";
             txt1.FontSize = 12;
             txt1.FontWeight = FontWeights.Bold;
             Grid.SetColumn(txt1, 0);
             DynamicGrid.Children.Add(txt1);
    
    
             TextBlock txt2 = new TextBlock();
             txt2.Text = "Colm 2";
             txt2.FontSize = 12;
             txt2.FontWeight = FontWeights.Bold;
             Grid.SetColumn(txt2, 1);
             DynamicGrid.Children.Add(txt2);
         }

And added another Grid which has Brown Background for testing in xaml:

   <StackPanel Name="mySp" Width="{Binding RelativeSource={RelativeSource AncestorLevel=1,AncestorType=Window,Mode=FindAncestor},Path=Width}" Height="450" Background="LightPink">
         <Grid ShowGridLines="True" Margin="0 100 0 0" Background="Brown">
             <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="1*"></ColumnDefinition>
                 <ColumnDefinition Width="60"></ColumnDefinition>
             </Grid.ColumnDefinitions>
             <TextBlock Grid.Column="0" Text="Colm 1" FontSize="12" FontWeight="Bold"></TextBlock>
             <TextBlock Grid.Column="2" Text="Colm 2" FontSize="12" FontWeight="Bold"></TextBlock>
         </Grid>
     </StackPanel>

The Brown Grid and LightSteelBlue Grid have the same effect like the below pictire shown:
84700-2.gif

By the way, if I misunderstand your question, please point out.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2.gif (352.5 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello. Thanks for the help. That partially answered my question I have one more very irritating problem I attached an image of it. I have a scroll viewer and I added a stack panel to its children then added the DynamicGrid to that. I cannot seem to set the width of the stack panel to the width of the scroll viewer. The scroll viewer is set to the right size but when I try to apply that width to the stack panel and or the DynamicGrid width it doesnt work and using horizontal alignment.Stretch doesn't work either. The image I attached shows that the dyamic grid and stack panels width is not working properly. All my code is dynamic except for the scroll viewer which I made through the designer.





86332-needhelp.png


0 Votes 0 ·
needhelp.png (11.8 KiB)

@JohnOstler-6145
I'm glad my answer was helpful to you. For your one more very irritating problem, it is a good way for you to create a new Thread and provide detailed information that will help other forum members read it. For your new problem, please add more info about your scroll viewer and how did you set the size.

0 Votes 0 ·