只是一个供参考的东西,我发现,工作
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*}">
什么ListView控件的宽度和高度?我应该为两个属性定义一个固定值,还是只说“自动”? – woodykiddy 2011-06-14 04:21:10
是的,只是放弃width属性并让它填充列。同样,如果您在网格中定义行并希望其垂直自动调整大小,那么也适用于高度。 – 2011-06-14 04:23:20
如果我用ListPanel包装Listview,还需要改变什么?似乎StackPanel不接受'*'。 – woodykiddy 2011-06-14 04:33:13