简体   繁体   English

从代码后面绑定高度

[英]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). 我有一个自定义GridView之类的控件,该控件由Grid面板制成,每行都有几个控件(所有控件都来自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. 我正在尝试将每行中所有控件的Height单向绑定到RowDefenition ActualHeight属性。 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. 尝试将控件的VerticalAlignment设置为Stretch ,使其扩展以填充所有可用空间。

Also, I don't think RowDefinition has an ActualHeight property, so the binding is probably evaluating to nothing. 另外,我不认为RowDefinition具有ActualHeight属性,因此绑定可能没有任何结果。 You'd need to bind to the ActualHeight of your control, however that value isn't know until after it's been Rendered. 您需要绑定到控件的ActualHeight ,但是只有在渲染之后才知道该值。 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. 我想您可以使用Dispatcher.Invoke在控件呈现后运行某些东西,弄清楚哪个是该行中最高的控件,并将所有项目设置为该高度。

You can bind your control's height(or any property) with other control's height with C# code. 您可以使用C#代码将控件的高度(或任何属性)与其他控件的高度绑定。 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. 您应该在XAML中而不是在代码隐藏中进行Height绑定。

Use the HorizontalAlignment="Stretch" VerticalAlignment="Stretch" property on your control. 使用控件上的Horizo​​ntalAlignment =“ Stretch” VerticalAlignment =“ Stretch”属性。

Alternative, use Height="{Binding ElementName=LayoutRoot, Path=ActualHeight} to bind with the actual height of your maingrid. 或者,使用Height =“ {Binding ElementName = LayoutRoot,Path = ActualHeight}绑定主网格的实际高度。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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