C# WPF在DataTemplate中将RowDefinition.Height属性绑定到另一个Height=Auto的RowDefinition
我想在WPF数据模板的网格中创建3行。第一个设置为高度=自动,第二个填充可用空间,第三个等于第一个。我尝试过绑定到elementname,但似乎不起作用C# WPF在DataTemplate中将RowDefinition.Height属性绑定到另一个Height=Auto的RowDefinition,c#,wpf,xaml,C#,Wpf,Xaml,我想在WPF数据模板的网格中创建3行。第一个设置为高度=自动,第二个填充可用空间,第三个等于第一个。我尝试过绑定到elementname,但似乎不起作用 <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" x:Name="definition" /> <RowDefinition Height="*" /> <RowDefi
<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>
在这个例子中,我希望第三行的高度也是100px。有什么建议吗?
行定义。实际高度
不是一个实际的依赖属性,这意味着您的绑定不会得到任何有关正在更改的实际高度的“更新”
您可以遵循以下模式:
<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>
从理论上讲,这样做的原因很简单:RowDefinition.ActualHeight==FirstRow.ActualHeight
(默认情况下,它应该填充可用空间)
或者,只需窃取RowDefinition,或者创建您自己的CustomRowDefinition,它可以实现名为ActualHeight
的依赖项属性,并触发更新
试试这个:
<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>
Auto表示适合内容……您没有在第0行中添加控件内容,因此其实际高度在此处为0。