How to set the width of Datagridcolumn based on the the width of the content placed in Datagrid cell





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I want to set the width of Datagridcolumn such that the columns width should size based on the content placed in Datagrid cell such that there should be
equal space between columns.



This is what i tried so far:



<DataGrid  x:Name="MyGrid"  AutoGenerateColumns="False" BorderBrush="Transparent"  
GridLinesVisibility="None" Background="Transparent" RowHeaderWidth="0" >
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="FontFamily" Value="Segoe UI"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Padding" Value="0,5"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header = "Column 1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding SomeText}" Width=120 TextWrapping="WrapWithOverflow" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

<DataGridTemplateColumn ..../>
<DataGridTemplateColumn ..../>
</DataGrid.Columns>
</Datagrid>


Some thing like this (as shown below) where there is equal space between columns and the columns are adjusted based on the width of content defined in data-grid cell.



enter image description here










share|improve this question































    0















    I want to set the width of Datagridcolumn such that the columns width should size based on the content placed in Datagrid cell such that there should be
    equal space between columns.



    This is what i tried so far:



    <DataGrid  x:Name="MyGrid"  AutoGenerateColumns="False" BorderBrush="Transparent"  
    GridLinesVisibility="None" Background="Transparent" RowHeaderWidth="0" >
    <DataGrid.Resources>
    <Style TargetType="{x:Type DataGridColumnHeader}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="FontFamily" Value="Segoe UI"/>
    <Setter Property="FontWeight" Value="Normal"/>
    <Setter Property="FontSize" Value="20"/>
    <Setter Property="Padding" Value="0,5"/>
    </Style>
    </DataGrid.Resources>
    <DataGrid.Columns>
    <DataGridTemplateColumn Header = "Column 1">
    <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
    <TextBlock Text="{Binding SomeText}" Width=120 TextWrapping="WrapWithOverflow" />
    </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

    <DataGridTemplateColumn ..../>
    <DataGridTemplateColumn ..../>
    </DataGrid.Columns>
    </Datagrid>


    Some thing like this (as shown below) where there is equal space between columns and the columns are adjusted based on the width of content defined in data-grid cell.



    enter image description here










    share|improve this question



























      0












      0








      0








      I want to set the width of Datagridcolumn such that the columns width should size based on the content placed in Datagrid cell such that there should be
      equal space between columns.



      This is what i tried so far:



      <DataGrid  x:Name="MyGrid"  AutoGenerateColumns="False" BorderBrush="Transparent"  
      GridLinesVisibility="None" Background="Transparent" RowHeaderWidth="0" >
      <DataGrid.Resources>
      <Style TargetType="{x:Type DataGridColumnHeader}">
      <Setter Property="Background" Value="Transparent" />
      <Setter Property="FontFamily" Value="Segoe UI"/>
      <Setter Property="FontWeight" Value="Normal"/>
      <Setter Property="FontSize" Value="20"/>
      <Setter Property="Padding" Value="0,5"/>
      </Style>
      </DataGrid.Resources>
      <DataGrid.Columns>
      <DataGridTemplateColumn Header = "Column 1">
      <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
      <TextBlock Text="{Binding SomeText}" Width=120 TextWrapping="WrapWithOverflow" />
      </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>

      <DataGridTemplateColumn ..../>
      <DataGridTemplateColumn ..../>
      </DataGrid.Columns>
      </Datagrid>


      Some thing like this (as shown below) where there is equal space between columns and the columns are adjusted based on the width of content defined in data-grid cell.



      enter image description here










      share|improve this question
















      I want to set the width of Datagridcolumn such that the columns width should size based on the content placed in Datagrid cell such that there should be
      equal space between columns.



      This is what i tried so far:



      <DataGrid  x:Name="MyGrid"  AutoGenerateColumns="False" BorderBrush="Transparent"  
      GridLinesVisibility="None" Background="Transparent" RowHeaderWidth="0" >
      <DataGrid.Resources>
      <Style TargetType="{x:Type DataGridColumnHeader}">
      <Setter Property="Background" Value="Transparent" />
      <Setter Property="FontFamily" Value="Segoe UI"/>
      <Setter Property="FontWeight" Value="Normal"/>
      <Setter Property="FontSize" Value="20"/>
      <Setter Property="Padding" Value="0,5"/>
      </Style>
      </DataGrid.Resources>
      <DataGrid.Columns>
      <DataGridTemplateColumn Header = "Column 1">
      <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
      <TextBlock Text="{Binding SomeText}" Width=120 TextWrapping="WrapWithOverflow" />
      </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>

      <DataGridTemplateColumn ..../>
      <DataGridTemplateColumn ..../>
      </DataGrid.Columns>
      </Datagrid>


      Some thing like this (as shown below) where there is equal space between columns and the columns are adjusted based on the width of content defined in data-grid cell.



      enter image description here







      c# wpf wpf-controls wpfdatagrid datagridviewcolumn






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 12:37







      user42067

















      asked Nov 23 '18 at 5:55









      user42067user42067

      120214




      120214
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Here is what you can try, string width can be calculated using the TextFomratter class(assuming your grid contains only text),
          So you would need to pass have a way to calculate the width of the biggest column, which can be calculated by using a binding - RelativePath,etc



          private Size MeasureString(string candidate)
          {
          var formattedText = new FormattedText(
          candidate,
          CultureInfo.CurrentCulture,
          FlowDirection.LeftToRight,
          new Typeface(this.textBlock.FontFamily, this.textBlock.FontStyle, this.textBlock.FontWeight, this.textBlock.FontStretch),
          this.textBlock.FontSize,
          Brushes.Black,
          new NumberSubstitution(),
          1);

          return new Size(formattedText.Width, formattedText.Height);
          }


          You can use the above code with a MultiConverter to get the a reference to the parent Grid object that you can use with a forloop to calculate the highest width at the current time, from there you might need to update all columns with the new column width.



          You would need to have text + Grid parent object as the parameters for the multiconverter.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441299%2fhow-to-set-the-width-of-datagridcolumn-based-on-the-the-width-of-the-content-pla%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Here is what you can try, string width can be calculated using the TextFomratter class(assuming your grid contains only text),
            So you would need to pass have a way to calculate the width of the biggest column, which can be calculated by using a binding - RelativePath,etc



            private Size MeasureString(string candidate)
            {
            var formattedText = new FormattedText(
            candidate,
            CultureInfo.CurrentCulture,
            FlowDirection.LeftToRight,
            new Typeface(this.textBlock.FontFamily, this.textBlock.FontStyle, this.textBlock.FontWeight, this.textBlock.FontStretch),
            this.textBlock.FontSize,
            Brushes.Black,
            new NumberSubstitution(),
            1);

            return new Size(formattedText.Width, formattedText.Height);
            }


            You can use the above code with a MultiConverter to get the a reference to the parent Grid object that you can use with a forloop to calculate the highest width at the current time, from there you might need to update all columns with the new column width.



            You would need to have text + Grid parent object as the parameters for the multiconverter.






            share|improve this answer




























              0














              Here is what you can try, string width can be calculated using the TextFomratter class(assuming your grid contains only text),
              So you would need to pass have a way to calculate the width of the biggest column, which can be calculated by using a binding - RelativePath,etc



              private Size MeasureString(string candidate)
              {
              var formattedText = new FormattedText(
              candidate,
              CultureInfo.CurrentCulture,
              FlowDirection.LeftToRight,
              new Typeface(this.textBlock.FontFamily, this.textBlock.FontStyle, this.textBlock.FontWeight, this.textBlock.FontStretch),
              this.textBlock.FontSize,
              Brushes.Black,
              new NumberSubstitution(),
              1);

              return new Size(formattedText.Width, formattedText.Height);
              }


              You can use the above code with a MultiConverter to get the a reference to the parent Grid object that you can use with a forloop to calculate the highest width at the current time, from there you might need to update all columns with the new column width.



              You would need to have text + Grid parent object as the parameters for the multiconverter.






              share|improve this answer


























                0












                0








                0







                Here is what you can try, string width can be calculated using the TextFomratter class(assuming your grid contains only text),
                So you would need to pass have a way to calculate the width of the biggest column, which can be calculated by using a binding - RelativePath,etc



                private Size MeasureString(string candidate)
                {
                var formattedText = new FormattedText(
                candidate,
                CultureInfo.CurrentCulture,
                FlowDirection.LeftToRight,
                new Typeface(this.textBlock.FontFamily, this.textBlock.FontStyle, this.textBlock.FontWeight, this.textBlock.FontStretch),
                this.textBlock.FontSize,
                Brushes.Black,
                new NumberSubstitution(),
                1);

                return new Size(formattedText.Width, formattedText.Height);
                }


                You can use the above code with a MultiConverter to get the a reference to the parent Grid object that you can use with a forloop to calculate the highest width at the current time, from there you might need to update all columns with the new column width.



                You would need to have text + Grid parent object as the parameters for the multiconverter.






                share|improve this answer













                Here is what you can try, string width can be calculated using the TextFomratter class(assuming your grid contains only text),
                So you would need to pass have a way to calculate the width of the biggest column, which can be calculated by using a binding - RelativePath,etc



                private Size MeasureString(string candidate)
                {
                var formattedText = new FormattedText(
                candidate,
                CultureInfo.CurrentCulture,
                FlowDirection.LeftToRight,
                new Typeface(this.textBlock.FontFamily, this.textBlock.FontStyle, this.textBlock.FontWeight, this.textBlock.FontStretch),
                this.textBlock.FontSize,
                Brushes.Black,
                new NumberSubstitution(),
                1);

                return new Size(formattedText.Width, formattedText.Height);
                }


                You can use the above code with a MultiConverter to get the a reference to the parent Grid object that you can use with a forloop to calculate the highest width at the current time, from there you might need to update all columns with the new column width.



                You would need to have text + Grid parent object as the parameters for the multiconverter.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 7:38









                mahlatsemahlatse

                1,037519




                1,037519
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441299%2fhow-to-set-the-width-of-datagridcolumn-based-on-the-the-width-of-the-content-pla%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                    ComboBox Display Member on multiple fields

                    Is it possible to collect Nectar points via Trainline?