2012-04-25 65 views
31

我正在使用ListView控件而不是DataGrid在我的WPF應用程序中。我想*寬度爲我的ListView.GridViewColumn,但每當我提供*寬度到ListView.GridViewColumn,它給我一個編譯時錯誤。請建議我如何提供*寬度至ListView.GridViewColumn,以便在最大化屏幕時ListView.GridViewColumn可以自動填充額外空間。ListView.GridViewColumn(*)width

對此的任何幫助將不勝感激。由於

回答

57

請嘗試這種解決辦法:

<ListView> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn Header="column1" x:Name="col1"/> 
      <!--Column that shall resize: Width is set to the Actual Width of the helper field defined below--> 
      <GridViewColumn Header="column2" 
          Width="{Binding ElementName=helperField, Path=ActualWidth}"/> 
     </GridView> 
    </ListView.View> 
    Test Text 
</ListView> 

<!--This is the hidden helper Grid which does the resizing --> 
<Grid Visibility="Hidden"> 
    <Grid.ColumnDefinitions> 
     <!--Width is bound to width of the first GridViewColumn --> 
     <ColumnDefinition Width="{Binding ElementName=col1, Path=ActualWidth}"/> 
     <!--Width is set to "Fill"--> 
     <ColumnDefinition Width="*"/> 
     <!--Correction Width--> 
     <ColumnDefinition Width="10"/> 
    </Grid.ColumnDefinitions> 
    <!--This is the hidden helper Field which is used to bind to, using the "Fill" column of the helper grid--> 
    <Grid Grid.Column="1" x:Name="helperField"/> 
</Grid> 

你也可以找到在以下鏈接一些其他的解決辦法:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/3ee5696c-4f26-4e30-8891-0e2f95d69623/

+0

感謝比拉爾哈什米用於數據HTML視圖。但是在上面的xaml方法中,我應該在我的xaml頁面中放置一行代碼,以便它會影響ListView列。使用IValueConverter時,它很好地工作,當第一次屏幕加載時,但是當我最大化屏幕或調整大小屏幕時,它不會相應地調整列寬。 – Yogesh 2012-04-25 05:32:43

+0

也許這可能有助於http://stackoverflow.com/questions/560581/how-to-autosize-and-right-align-gridviewcolumn-data-in-wpf – Klaus78 2012-04-25 05:47:21

+0

你可以把它放在你正在顯示的xaml頁面的任何地方你的ListView。可以說在頁面主柵格的行= 0和列= 0。 – 2012-04-25 09:52:33

3

我張貼這種這裏我的做法是有一點不同(但發現它是非常可靠的,並允許百分比寬度列https://stackoverflow.com/a/10526024/41211),因爲我嘗試了上述,並發現我的devenv.exe處理最大化,因爲它不斷嘗試重新評估我的設計師視圖與bove動態綁定。