2

我正在用行以编程方式填充 WPF 网格。每行都分配有一个用户控件。此用户控件包含一个 Grid 本身,恰好有 1 行和 3 列。第 3 列可以包含一个 TextBox、一个 ComboBox 或 ListBox 中可变数量的 CheckBox。

虽然我已将用户控件中的行高和包含用户控件的网格设置为自动,但行的高度不会扩展。较低的复选框就消失了。我尝试了不同的东西,但还没有运气。有任何想法吗?

4

3 回答 3

3

要从 XAML 设置“自动”宽度或高度,您可以使用 double.Nan 代替“Value="Auto"”。

<DataGrid>
   <DataGrid.RowStyle>
      <Style TargetType="{x:Type DataGridRow}">
         <Setter Property="Height" Value="{x:Static sys:Double.NaN}"/>
      </Style>
   </DataGrid.RowStyle
</DataGrid>

不要忘记添加

xmlns:sys="clr-namespace:System;assembly=mscorlib" 

在 XAML 的头部使用“sys”。

于 2014-06-03T08:18:26.243 回答
0

您可以在自己后面的代码中计算行高...

myGrid.RowHeight = myGrid.RenderSize.Height / myGrid.ItemsSource.Cast<MyRowDef>().Count();
于 2012-10-17T13:38:21.427 回答
-2

嗨试试这样

    <DataGrid>
        <DataGrid.RowStyle>
            <Style TargetType="{x:Type DataGridRow}">
                <Setter Property="Height" Value="Auto"/>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>

在代码隐藏中

DataGrid dg=new DataGrid();
        Style style = new Style(typeof(DataGridRow));
        style.Setters.Add(new Setter(DataGridRow.HeightProperty,new GridLength(20, GridUnitType.Auto)));
        dg.RowStyle=style;

我希望这将有所帮助。

于 2012-07-13T18:43:15.257 回答