none
How to remove the last blank/extral column in ListView's GridView? RRS feed

  • 问题

  • Hi, Everyone.

    I am a new one to use WPF, I got some problmes in using ListView's GridView, Does anyone knows how to remove the last blank/extral column in ListView's GridView?

    Following is my code:

    <Grid>
            <ListView x:Name="ListView">
                <ListView.View>
                    <GridView AllowsColumnReorder="False">
                        <GridViewColumn Width="Auto">
                            <GridViewColumnHeader HorizontalContentAlignment="Left">Operation
                            </GridViewColumnHeader>
                        </GridViewColumn>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Date}" Width="Auto">
                            <GridViewColumnHeader HorizontalContentAlignment="Left">Date</GridViewColumnHeader>
                        </GridViewColumn>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Operator}" >
                            <GridViewColumnHeader HorizontalContentAlignment="Left">Operator</GridViewColumnHeader>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>

    To show the issue here, I want to remove the last extra column, just like the picture:

    2012年2月1日 6:14

答案

  • Well, since the sample set the width of the last column before loading the data; so the last column will extend to the outside of the ListView after showing the data.

    I modified his solution, add the event handler on the ListView to handle the size change:

      <Grid>
        <ListView x:Name="ListView" ItemsSource="{Binding}" SizeChanged="ListView_SizeChanged" Loaded="ListView_Loaded">
          <ListView.View>
            <GridView AllowsColumnReorder="False">
              <GridViewColumn Width="Auto">
                <GridViewColumnHeader HorizontalContentAlignment="Left" Content="Operation"/>
              </GridViewColumn>
              <GridViewColumn DisplayMemberBinding="{Binding Path=Date, Mode=OneWay}" Width="Auto">
                <GridViewColumnHeader HorizontalContentAlignment="Left" Content="Date"/>
              </GridViewColumn>
              <GridViewColumn DisplayMemberBinding="{Binding Path=Operator}" Width="Auto">
                <GridViewColumnHeader HorizontalContentAlignment="Left" Content="Operator" />
              </GridViewColumn>
            </GridView>
          </ListView.View>
        </ListView>
      </Grid>

     

    C#:

        private void ListView_Loaded(object sender, RoutedEventArgs e)
        {
          Resize(sender);
        }
    
        private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
        {
          Resize(sender);
        }
    
        void Resize(object sender)
        {
          ListView listview = sender as ListView;
          GridView view = listview.View as GridView;
          GridViewColumn column = view.Columns.Last() as GridViewColumn;
          double total = 0;
          for (int i = 0; i < view.Columns.Count - 1; i++)
          {
            total += view.Columns[i].ActualWidth +5;
          }
          column.Width = listview.ActualWidth - total;
        }


     


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年2月2日 7:55
    版主

全部回复

  • This thread answered it: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/3ee5696c-4f26-4e30-8891-0e2f95d69623/ WPF does not implement it, we should deisgn one converter to convert the size for the width of the last gridview column
    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    2012年2月1日 8:49
    版主
  • Thank you Bob, I follow the link method, and this fix seems to be resolve this issue, but i try it, it does not work as my expect, when the code finish runing IValueConvert, there will have a scroll bar, actually, the last column is still fill the last column, the blank column is still existing... 
    2012年2月1日 10:24
  • Well, since the sample set the width of the last column before loading the data; so the last column will extend to the outside of the ListView after showing the data.

    I modified his solution, add the event handler on the ListView to handle the size change:

      <Grid>
        <ListView x:Name="ListView" ItemsSource="{Binding}" SizeChanged="ListView_SizeChanged" Loaded="ListView_Loaded">
          <ListView.View>
            <GridView AllowsColumnReorder="False">
              <GridViewColumn Width="Auto">
                <GridViewColumnHeader HorizontalContentAlignment="Left" Content="Operation"/>
              </GridViewColumn>
              <GridViewColumn DisplayMemberBinding="{Binding Path=Date, Mode=OneWay}" Width="Auto">
                <GridViewColumnHeader HorizontalContentAlignment="Left" Content="Date"/>
              </GridViewColumn>
              <GridViewColumn DisplayMemberBinding="{Binding Path=Operator}" Width="Auto">
                <GridViewColumnHeader HorizontalContentAlignment="Left" Content="Operator" />
              </GridViewColumn>
            </GridView>
          </ListView.View>
        </ListView>
      </Grid>

     

    C#:

        private void ListView_Loaded(object sender, RoutedEventArgs e)
        {
          Resize(sender);
        }
    
        private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
        {
          Resize(sender);
        }
    
        void Resize(object sender)
        {
          ListView listview = sender as ListView;
          GridView view = listview.View as GridView;
          GridViewColumn column = view.Columns.Last() as GridViewColumn;
          double total = 0;
          for (int i = 0; i < view.Columns.Count - 1; i++)
          {
            total += view.Columns[i].ActualWidth +5;
          }
          column.Width = listview.ActualWidth - total;
        }


     


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年2月2日 7:55
    版主