我有一个列表视图
<ListView Grid.Row="4" Grid.ColumnSpan="2"
Margin="{Binding ElementName=BorderContainer, Path=Margin}"
FontSize="{DynamicResource PlaceHolderFontSize}"
ItemContainerStyle="{StaticResource ListItemContainerStyle}"
Foreground="Black"
>
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource BorderlessHeader}">
<GridViewColumn Header="Last Backup Date/Time" HeaderTemplate="{StaticResource BlueHeader}"
Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}, Converter={StaticResource ColumnWidth},ConverterParameter=221}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Date}" ToolTip="{Binding Date}" FontSize="{DynamicResource ContentFontSize}" Style="{StaticResource GridColumText}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
如代码中所述,使用转换器计算标题宽度。在我的转换器里面使用如下
public object Convert(object value, Type target Type, object parameter, Culture Info culture)
{
ListView listview = value as ListView;
double width = listview.ActualWidth;
但listview.actualwidth总是给出“0”作为结果。为什么会这样?任何想法?
答案 0 :(得分:1)
为什么会这样?
可能是因为ListView
方法在调用Convert
方法时实际上具有0的实际宽度。
任何想法?
您可以使用与MultiBinding
本身及其IMultiValueConverter
属性绑定的ListView
和ActualWidth
:
<GridViewColumn Header="Last Backup Date/Time" HeaderTemplate="{StaticResource BlueHeader}">
<GridViewColumn.Width>
<MultiBinding Converter="{StaticResource ColumnWidth}" ConverterParameter="221">
<Binding Path="." RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ListView}" />
<Binding Path="ActualWidth" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ListView}" />
</MultiBinding>
</GridViewColumn.Width>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Date}" ToolTip="{Binding Date}" FontSize="{DynamicResource ContentFontSize}" Style="{StaticResource GridColumText}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
public class ColumnWidthConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
ListView listview = values[0] as ListView;
double width = listview.ActualWidth;
if (width == 0)
return (double)parameter;
//...
return 100.0;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
然后,只要更新ActualWidth属性,就应该调用转换器。
答案 1 :(得分:0)
如果您有 4 Columns
,则可以尝试以下操作:
<ListView x:Name="listView">
<ListView.View>
<GridView>
<GridViewColumn Width="{Binding ElementName=listView, Path=ActualWidth, Converter={StaticResource ColumnWidthConverter},ConverterParameter=4}">
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
public class ColumnWidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int columns;
if (int.TryParse(parameter.ToString(), out columns))
{
double width;
if (columns > 0 && double.TryParse(value.ToString(), out width))
{
return width / columns;
}
}
return 100.0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
如果您想要更多动态,可以撰写Behavior
并将其附加到ListView
。在那里,您可以根据Columns
计数动态计算值。