只是一个关于我发现有效的东西的仅供参考
Here is a Value Converter that can be used with a ListView and allow star column sizing
来自文章:
/// <summary>
/// Calculates the column width required to fill the view in a GridView
/// For usage examples, see http://leghumped.com/blog/2009/03/11/wpf-gridview-column-width-calculator/
/// </summary>
public class WidthConverter : IValueConverter {
/// <summary>
/// Converts the specified value.
/// </summary>
/// <param name="value">The parent Listview.</param>
/// <param name="type">The type.</param>
/// <param name="parameter">
/// If no parameter is given, the remaning with will be returned.
/// If the parameter is an integer acts as MinimumWidth, the remaining with will be returned only if it's greater than the parameter
/// If the parameter is anything else, it's taken to be a percentage. Eg: 0.3* = 30%, 0.15* = 15%
/// </param>
/// <param name="culture">The culture.</param>
/// <returns>The width, as calculated by the parameter given</returns>
public object Convert(object value, Type type, object parameter, CultureInfo culture) {
if(value == null) return null;
ListView listView = value as ListView;
GridView grdView = listView.View as GridView;
int minWidth = 0;
bool widthIsPercentage = parameter != null && !int.TryParse(parameter.ToString(), out minWidth);
if(widthIsPercentage) {
string widthParam = parameter.ToString();
double percentage = double.Parse(widthParam.Substring(0, widthParam.Length - 1));
return listView.ActualWidth * percentage;
} else {
double total = 0;
for(int i = 0; i < grdView.Columns.Count - 1; i++) {
total += grdView.Columns[i].ActualWidth;
}
double remainingWidth = listView.ActualWidth - total;
if(remainingWidth > minWidth) { // fill the remaining width in the ListView
return remainingWidth;
} else { // fill remaining space with MinWidth
return minWidth;
}
}
}
public object ConvertBack(object o, Type type, object parameter, CultureInfo culture) {
throw new NotSupportedException();
}
}
如果你不带参数调用它,它将占据ListView中剩余的宽度:
// fills remaining width in the ListView
<GridViewColumn Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListView}},Converter={StaticResource WidthConverter}}">
如果使用整数作为参数,则该值将作为最小宽度
// fills remaining width in the ListView, unless the remaining width is less than the parameter
<GridViewColumn Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListView}},Converter={StaticResource WidthConverter},ConverterParameter=200}">
或者,可以用星号指定一个GridView类型的宽度,会返回ListView的百分比宽度
// calculates 30% of the ListView width
<GridViewColumn Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListView}},Converter={StaticResource WidthConverter},ConverterParameter=0.3*}">