Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to autosize and right-align GridViewColumn data in WPF?
    primarykey
    data
    text
    <p>How can I:</p> <ul> <li>right-align the text in the ID column</li> <li>make each of the columns auto size according to the text length of the cell with the longest visible data? </li> </ul> <p>Here is the code:</p> <pre><code>&lt;ListView Name="lstCustomers" ItemsSource="{Binding Path=Collection}"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}" Width="40"/&gt; &lt;GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="100" /&gt; &lt;GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}"/&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; </code></pre> <h2>partial answer:</h2> <p>Thanks Kjetil, the GridViewColumn.CellTemplate works well and the Auto Width works of course but when the ObservativeCollection "Collection" is updated with longer-than-column-width data, the column sizes do not update themselves so that is only a solution for the initial display of data:</p> <pre><code>&lt;ListView Name="lstCustomers" ItemsSource="{Binding Path=Collection}"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Header="ID" Width="Auto"&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Id}" TextAlignment="Right" Width="40"/&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="Auto" /&gt; &lt;GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" Width="Auto"/&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload