如何以编程方式创建 WPF Grid.RowDefintion.Height 绑定

How to programmatically create WPF Grid.RowDefintion.Height binding

这应该很简单,但我卡住了!如何在代码 中创建以下多重绑定 并将其应用于给定的行定义:

<Grid.RowDefinitions>
    <RowDefinition Height="*"/>
        <RowDefinition>
            <RowDefinition.Height>
                <MultiBinding Converter="{StaticResource MyMultiConverter}">
                    <Binding ElementName="obj1" Path="x"/>
                    <Binding ElementName="obj2" Path="y"/> 
                </MultiBinding>
            </RowDefinition.Height>   
        </RowDefinition>
 </Grid.RowDefinitions>

谢谢!

好了:

//Create binding
var binding = new MultiBinding
{
    Converter = new MyMultiConverter()
};
binding.Bindings.Add(new Binding("x") { ElementName = "obj1" });
binding.Bindings.Add(new Binding("y") { ElementName = "obj2" });

//create RowDefinition
var definition = new RowDefinition();
//set binding on HeightProperty
definition.SetBinding(RowDefinition.HeightProperty, binding);

//'myGrid' is the name of the grid instance
//add RowDefinition to grid
myGrid.RowDefinitions.Add(definition);

为了能够在代码隐藏中处理网格,请向网格添加名称:

<Grid Name="myGrid">
    ...
</Grid>