c# - 如何绑定(bind)网格行高?

标签 c# .net wpf xaml data-binding

我有 2 个带分离器的相同网格:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Button Grid.Row="0" />
    <Button Grid.Row="2" />
    <GridSplitter Grid.Row="1" Height="4" HorizontalAlignment="Stretch" />
</Grid>

如何让它们同步调整大小?这样两个网格的行高将相同。

最佳答案

看起来我可以通过简单的绑定(bind)来做到这一点:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="{Binding Path=MySize, Mode=TwoWay}" />
        <RowDefinition Height="auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Button Grid.Row="0" />
    <Button Grid.Row="2" />
    <GridSplitter Grid.Row="1" Height="4" HorizontalAlignment="Stretch" />
</Grid>

MySize 在哪里

private GridLength mySize;
public GridLength MySize
{
    get { return mySize; }
    set
    {
        if (mySize == value) return;
        mySize = value;
        OnPropertyChanged("MySize");
    }
}

注意: Mode=TwoWay 是必需的,因为与其他控件不同,RowDefinition 不会将其模式默认为 TwoWay

关于c# - 如何绑定(bind)网格行高?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8326118/

相关文章:

c# - 您将如何重构此 LINQ 代码?

WPF 调度程序 - 突然出现 "stuck"并且在按下某个键之前不处理操作

c# - WPF 中的 session ?

c# - 将用户控件添加到列表框

c# - 如何避免多个 $(document).ready()

.net - 用文本行填充多行编辑框的方法是什么

c# - 将List<>写入数据库asp.net

.net - Entity Framework 4.0 创建从其他实体派生的新实体

c# - 替换文本中的非字母数字字符

c# - 在 C# 中使用锁定位写入图像