Search code examples
c#xamlbindingcode-behind

Convert XAML Binding to C#


I am building some dynamic controls and would like to convert the following XAML to C#...

Height="{Binding ElementName=GridGroup1, Path=ActualHeight}">

The above forms part of a dynamically built Border control that I would like to set so that it has the same height as a dynamically built Grid.

Any ideas on how I can do this please? All the examples I have found seem to be incomplete.

Thanks,

Paul.

Amendment... Ok here is the complete XAML...

<Grid x:Name="GridGroup1" HorizontalAlignment="Left"
                  Margin="20,14,0,0"
                  Width="250"
                  VerticalAlignment="Top">
                <Border BorderThickness="1"
                        CornerRadius="5"
                        Background="{StaticResource PanelBackground}"
                        BorderBrush="{StaticResource PanelBorderBrush}"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        Margin="0,8,0,0"
                        Width="250"
                        Height="{Binding ElementName=GridGroup1, Path=ActualHeight}">
                    <Border.Effect>
                        <DropShadowEffect />
                    </Border.Effect>
                </Border>


Solution

  • Something like:

        Binding binding = new Binding();
        binding.Source = GridGroup1;
        binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        binding.Path = new PropertyPath("ActualHeight");
        MyGridBorder.SetBinding(Border.HeightProperty, binding);