WPF 将 RowDefinition.Height 属性绑定到另一个 RowDefinition,在 DataTemplate 中 Height=Auto

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24801465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:10:05  来源:igfitidea点击:

WPF Bind RowDefinition.Height property to another RowDefinition with Height=Auto within DataTemplate

c#wpfxaml

提问by user1119308

I'd like to make 3 rows in a grid in a WPF datatemplate. The first is set to Height=Auto, the second fills the available space and the third is equal to the first. I've tried with binding to elementname, but that doesn't seem to work

我想在 WPF 数据模板的网格中创建 3 行。第一个设置为 Height=Auto,第二个填充可用空间,第三个等于第一个。我尝试绑定到 elementname,但这似乎不起作用

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" x:Name="definition" />
        <RowDefinition Height="*" />
        <RowDefinition Height="{Binding ElementName=definition, Path=ActualHeight}" />
    </Grid.RowDefinitions>

    <TextBox Grid.Row="0" Height="100" />
</Grid>

In this example I hope the height of the third row is also 100px. Any suggestions?

在这个例子中,我希望第三行的高度也是 100px。有什么建议?

回答by Erti-Chris Eelmaa

RowDefinition.ActualHeightis not an actually dependency property, which means your binding won't get any "updates" about ActualHeightbeing changed.

RowDefinition.ActualHeight不是实际的依赖属性,这意味着您的绑定不会获得任何关于ActualHeight被更改的“更新” 。

You can follow this pattern:

您可以遵循以下模式:

<Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
    <RowDefinition Height="{Binding ElementName=FirstRow, Path=ActualHeight}" />

    <Grid Grid.Row="0" x:Name="FirstRow" />
    <Grid Grid.Row="1" x:Name="SecondRow" />
    <Grid Grid.Row="2" x:Name="ThirdRow" />
</Grid.RowDefinitions>

The reason this should work theoretically, is simple enough: RowDefinition.ActualHeight == FirstRow.ActualHeight(by default it should fill available space)

这在理论上应该有效的原因很简单:(RowDefinition.ActualHeight == FirstRow.ActualHeight默认情况下它应该填充可用空间)

Alternatively, just steal the RowDefinition, and perhaps create your own CustomRowDefinition which can implement dependency property, called ActualHeight, and fire updates.

或者,只需窃取 RowDefinition,或者创建您自己的 CustomRowDefinition,它可以实现依赖属性,称为ActualHeight,并触发更新。

http://dotnetinside.com/in/type/PresentationFramework/RowDefinition/4.0.0.0

http://dotnetinside.com/in/type/PresentationFramework/RowDefinition/4.0.0.0

回答by Victory Jessie

Try this:

尝试这个:

<Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid Height="100" Background="Red" x:Name="definition"/>
    <Grid Background="Green" Grid.Row="1"/>
    <Grid Background="Blue" Grid.Row="2" Height="{Binding ElementName=definition, Path=ActualHeight}"/>
</Grid>