1

This should be simple, but I'm stuck! How to create the following multibinding in code and apply it to the given row definition:

<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>

Thanks!

Ivan Pelly
  • 147
  • 2
  • 12

1 Answers1

0

There you go:

//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);

To be able to handle the grid in codebehind you add a name to the grid:

<Grid Name="myGrid">
    ...
</Grid>
Anders
  • 1,590
  • 1
  • 20
  • 37
  • Perfect - thanks! The definition.SetBinding was the missing piece of the puzzle for me. – Ivan Pelly Apr 29 '15 at 18:43
  • If you ever stumple into a class which don't have the `SetBinding`, but still inherits from `DependencyObject`, then you can set bindings via `BindingOperations.SetBinding` instead. – Anders Apr 29 '15 at 18:59