【问题标题】:MinHeight on resizeable Expander可调整大小的扩展器上的 MinHeight
【发布时间】:2015-04-28 05:31:39
【问题描述】:

我正在尝试创建一种样式,通过将内容嵌入到带有 GridSplitter 的网格中,自动使我的所有扩展器可调整大小。

<Window.Resources>
    <Style TargetType="Expander">
        <Setter Property="Background" Value="Gold"/>
        <Setter Property="FontSize" Value="17"/>
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Grid Margin="0" Background="Red">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" MinHeight="{Binding ???}"/>
                            <RowDefinition Height="5"/>
                        </Grid.RowDefinitions>
                        <ContentControl Content="{Binding}" MinHeight="{Binding ???}"/>
                        <GridSplitter Grid.Row="1" Height="5" Background="Green" 
                               HorizontalAlignment="Stretch"
                               ResizeBehavior="PreviousAndCurrent"/>
                    </Grid>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Expander Grid.Row="0" IsExpanded="True" Header="Expander...">
        <Border Background="Blue" MinHeight="200" BorderThickness="5" BorderBrush="Black"/>
    </Expander>
    <Expander Grid.Row="1" IsExpanded="True" Header="Expander...">
        <Border Background="Blue" MinHeight="100" BorderThickness="5" BorderBrush="Black"/>
    </Expander>
</Grid>

我想不通的是如何将样式内部的 RowDefinition(或 ContentControl)的 MinHeight 绑定到 Expander 的 Content 的 MinHeight。

(注意,花哨的颜色只是为了看看是什么......)

【问题讨论】:

    标签: c# .net xaml


    【解决方案1】:

    我不确定您需要它的程度以及是否能解决您的问题:

    <Grid.RowDefinitions>
                <RowDefinition x:Name="GridRowDefinition" Height="Auto" MinHeight="20"/>
                <RowDefinition x:Name="GridRowDefinition2"  Height="Auto" MinHeight="20"/>
            </Grid.RowDefinitions>
    

    在风格上

    <Setter.Value>
                        <DataTemplate>
                            <Grid Margin="0" Background="Red">
                                <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" MinHeight="{Binding MinHeight, ElementName=GridRowDefinition}"/>
                                    <RowDefinition Height="5"/>
                                </Grid.RowDefinitions>
                            <ContentControl Content="{Binding}" MinHeight="{Binding MinHeight, ElementName=GridRowDefinition2}"/>
                                <GridSplitter Grid.Row="1" Height="5" Background="Green" 
                               HorizontalAlignment="Stretch"
                               ResizeBehavior="PreviousAndCurrent"/>
                            </Grid>
                        </DataTemplate>
                    </Setter.Value>
    

    因为如果您在扩展器内添加边框,我不明白为什么要将内容放置在扩展器中。希望对你有帮助

    【讨论】:

    • 扩展器内的边框只是一个占位符,用于测试嵌入内容,它可以是其他任何东西。不幸的是,我想绑定的 MinHeight 不是您在第一个 sn-p 中拥有的那个:它是 Content/Border 的 MinHeight。
    【解决方案2】:

    愚蠢的我:查看调试输出表明{Binding} 与内容相关(在本例中为边框)。因此,只需在 RowDefinition(在 Style.DataTemplate 内)上执行 {Binding MinHeight} 即可获得所需的效果。

    下一步:研究如何记忆和恢复高度:)

    【讨论】:

      猜你喜欢
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-10
      相关资源
      最近更新 更多