2011-02-17 120 views

回答

1

對於GridViewColumn,你有ActualWidth屬性,它不是一個依賴屬性,所以如果你直接綁定到它,當屬性發生變化時你將不會得到任何更新。然後你有寬度屬性這是一個依賴屬性,並會給你大多數情況下正確的值,但並不總是如自動大小時。結合這兩者應該會給你想要的結果。也就是說,聽在Width屬性的變化,並用MultiBinding返回ActualWidth的

的XAML

<WrapPanel ...> 
    <WrapPanel.Width> 
     <MultiBinding Converter="{StaticResource GridViewColumnWidthMultiConverter}"> 
      <Binding ElementName="listView" Path="View.Columns[0].Width"/> 
      <Binding ElementName="listView" Path="View.Columns[0].ActualWidth"/> 
     </MultiBinding> 
    </WrapPanel.Width> 
</WrapPanel> 
<ListView Name="listView" ...> 
    <ListView.View> 
     <GridView> 
      <GridView.Columns> 
       <GridViewColumn .../> 
       <GridViewColumn .../> 
       <GridViewColumn .../> 
       <!--...--> 
      </GridView.Columns> 
     </GridView> 
    </ListView.View> 
</ListView> 

GridViewColumnWidthMultiConverter

public class GridViewColumnWidthMultiConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     double width = (double)values[0]; 
     double actualWidth = (double)values[1]; 
     return actualWidth; 
    } 
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
相關問題