简体   繁体   中英

Binding Height from code behind

I have a custom GridView like control made of a Grid panel with several controls in each row (All of them from System.Windows.Controls).

The user requested that the row Height will grow unrestricted with the control's content. I'm having a problem to maintain the size of all the controls in each row with relation to the highest one.

I'm trying to one-way bind the Height of all the controls in each row to the RowDefenition ActualHeight property. but it is not working as I expect it to work (Each control keeps it's own size to the minimum)

Any help will be appreciated, thanks.

Here is the code where I'm trying to bind:

                RowDefinition rowDef = new RowDefinition();

                cellsGrid.RowDefinitions.Add(rowDef);
                rowDef.Name = "gvRow" + cellsGrid.RowDefinitions.Count;

                cellsGrid.Children.Add(controlToAdd);
                Grid.SetRow(controlToAdd, rowIndex);
                Grid.SetColumn(controlToAdd, columnIndex);

                Binding bindH = new Binding("ActualHeight");
                bindH.Mode = BindingMode.OneWay;
                bindH.Source = rowDef;
                BindingOperations.SetBinding(controlToAdd,RowDefinition.HeightProperty, bindH);

                controlToAdd.TabIndex = (totalTabIndex + 1); totalTabIndex++;
                cell.CellElement = controlToAdd;
                cell.EndEdit += new GridViewCell.GridViewEditHandler(cell_EndEdit);

Try setting your Control's VerticalAlignment to Stretch , so it expands to fill all available space.

Also, I don't think RowDefinition has an ActualHeight property, so the binding is probably evaluating to nothing. You'd need to bind to the ActualHeight of your control, however that value isn't know until after it's been Rendered. I suppose you could use Dispatcher.Invoke to run something after the controls have been rendered, figure out which is the tallest control in the row, and set all item to be that height.

You can bind your control's height(or any property) with other control's height with C# code. Here is the example.

Grid myGrid = new Grid();
ListBox myList = new ListBox(); //it could be any control 
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
     myList.DataContext = this;
     myList.SetBinding(UserControl.HeightProperty,"this.myGrid.ActualHeight");
}

this worked for me.

You should do the Height binding in XAML, not in codebehind.

Use the HorizontalAlignment="Stretch" VerticalAlignment="Stretch" property on your control.

Alternative, use Height="{Binding ElementName=LayoutRoot, Path=ActualHeight} to bind with the actual height of your maingrid.

<ItemsControl ItemsSource="{Binding items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="{x:Type local:Class1}">
            <Grid Height="{Binding YourHeightValue}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding prop1}"/>
                <TextBlock Grid.Column="1" Text="{Binding prop2}"/>
                <TextBlock Grid.Column="2" Text="{Binding prop3}"/>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel IsItemsHost="True"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM