【需求描述】Grid(一行一列)下有个StackPanel,StackPanel下有个ListView,如何让ListView与Grid等宽。要求自适应,不能写死宽高。
解法是通过binding+converter。
前台
<UserControl.Resources>
<local:WorkbenchWidthConverter x:Key="wwc"/>
</UserControl.Resources>
···
<Grid x:Name="CoreWorkbench" Margin="0,1,1,1">
···
<StackPanel x:Name="featureWorkbench" Visibility="Collapsed">
<ListView x:Name="listFeature" HorizontalAlignment="Center" VerticalAlignment="Center"
Width="{Binding ActualWidth, ElementName=CoreWorkbench, Converter={StaticResource wwc}}"
Height="{Binding ActualHeight, ElementName=CoreWorkbench}">
<ListView.View>
···
后台
// 为了调节特征页面的宽度
public class WorkbenchWidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double w = System.Convert.ToDouble(value);
return w;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}