XAML DockPanel最后一个可见子填充?

ercv8c1e  于 2023-04-18  发布在  其他
关注(0)|答案(3)|浏览(121)

我寻找的行为基本上是一个DockPanel,我希望最后一个孩子来填充可用空间。捕获的是,我希望它是最后一个可见的孩子。在我的情况下,我有两个视图,我想并排显示。
到目前为止,我已经尝试了将两种不同的样式应用到网格中,似乎都不起作用。我还尝试使用一个转换器,它在理论上似乎起作用(我可以使用double.NaN返回“Auto”),但我不确定如何从代码中返回宽度为“*”的值。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*">
            <ColumnDefinition.Style>
                <Style TargetType="{x:Type ColumnDefinition}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Visibility, ElementName=ccSomeItems}" Value="Collapsed">
                            <Setter Property="Width" Value="0" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ColumnDefinition.Style>
        </ColumnDefinition>
        <ColumnDefinition Width="*">
            <ColumnDefinition.Style>
                <Style TargetType="{x:Type ColumnDefinition}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding SomeOtherItems.AllOtherItems.Count, Converter={StaticResource IntegerToVisibilityConverter}}" Value="Collapsed">
                            <Setter Property="Width" Value="0" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ColumnDefinition.Style>
        </ColumnDefinition>
    </Grid.ColumnDefinitions>
    <ContentControl x:Name="ccSomeItems" Content="{Binding SomeItems}" Margin="4" Visibility="{Binding SomeItems.AllItems.Count, Converter={StaticResource IntegerToVisibilityConverter}}" />
    <ContentControl Grid.Column="1" Content="{Binding SomeOtherItems}" Margin="4" Visibility="{Binding SomeOtherItems.AllOtherItems.Count, Converter={StaticResource IntegerToVisibilityConverter}}" />
</Grid>
nxagd54h

nxagd54h1#

我能够运行雷切尔的评论,并找到一个使用转换器的解决方案。
XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{Binding Visibility, ElementName=SomeItems, Converter={StaticResource VisibilityToGridLengthConverter}}" />
        <ColumnDefinition Width="{Binding Visibility, ElementName=SomeOtherItems, Converter={StaticResource VisibilityToGridLengthConverter}}" />
    </Grid.ColumnDefinitions>
    <ContentControl x:Name="SomeItems" Content="{Binding SomeItems}" Visibility="{Binding SubPackages.AllPackages.Count, Converter={StaticResource IntegerToVisibilityConverter}}" />
    <ContentControl x:Name="SomeOtherItems" Grid.Column="1" Content="{Binding SomeOtherItems}" Visibility="{Binding Elements.AllElements.Count, Converter={StaticResource IntegerToVisibilityConverter}}" />
</Grid>

转换器:

[ValueConversion(typeof(Visibility), typeof(GridLength))]
public class VisibilityToGridLengthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (Visibility)value == Visibility.Collapsed ? new GridLength(0) : new GridLength(1, GridUnitType.Star);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
pn9klfpd

pn9klfpd2#

我发现了一个更简单的解决方案,那就是用一个Grid元素包围你想要填充可用空间的元素。这样,网格就成为了最后一个总是填充可用空间的元素。网格中的控件可以将它们的可见性设置为任何它们想要的。

<DockPanel Height="31" HorizontalAlignment="Left" Margin="82,107,0,0" Name="dockPanel1" VerticalAlignment="Top" Width="286" LastChildFill="True">
  <TextBlock Text="Text block" DockPanel.Dock="Left" />
  <Grid>
    <Button Content="Button1" Visibility="Visible" />
    <Button Content="Button2" Visibility="Collapsed" />
  </Grid>
</DockPanel>
ruoxqz4g

ruoxqz4g3#

我知道这是旧的,但我的解决方案是简单地将最后一个子项 Package 在一个StackPanel中,如下所示:

<DockPanel LastChildFill="True">
                <Grid DockPanel.Dock="Right">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50"/>
                        <ColumnDefinition Width="70"/> 
                    </Grid.ColumnDefinitions>
                    <!-- First item -->
                </Grid>
                <StackPanel>
                    <Grid Visibility="{Binding IsVisible, Converter={StaticResource BooleanVisibilityConverter}}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="60"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <!-- Last child variable visibility  -->
                    </Grid>
                    <Grid Visibility="{Binding IsVisble, Converter={StaticResource InverseBooleanVisibilityConverter}}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <!-- Also last child variable visiblity -->
                    </Grid>
                </StackPanel>
            </DockPanel>

相关问题