[英]WPF stretch rows vertically
Hello hello everyone大家好大家好
I got one problem recently.我最近遇到一个问题。
I tried put one datagrid into another datagrid.我尝试将一个数据网格放入另一个数据网格。
Main datagrid has two columns - text and template (here I put another datagrid).主数据网格有两列 - 文本和模板(这里我放了另一个数据网格)。 Inner datagrid has one text column.
内部数据网格有一个文本列。
The problem show herself when Main DG text cell's height exceed sum height of inner DG cells.当主 DG 文本单元格的高度超过内部 DG 单元格的总高度时,问题就会显现出来。
Something like that:类似的东西:
Image图片
My question is: Can I do something with gray rectangle?我的问题是:我可以用灰色矩形做点什么吗? Can I equally divide gray height between rows?
我可以在行之间平均划分灰度高度吗? Or simple stretch rows?
还是简单的拉伸行?
Or is there better way to do it?或者有更好的方法来做到这一点? Not only 2-level datagrid, but to 3,4,5-levels and for more complicated datagrids.
不仅是 2 级数据网格,还有 3、4、5 级以及更复杂的数据网格。
Simple example:简单的例子:
Window:窗户:
<DockPanel>
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" HeadersVisibility="None" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Col1}" Width="*"/>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Path=Col2}" AutoGenerateColumns="False" HeadersVisibility="None" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DockPanel>
Code behind:后面的代码:
public class Test1 : NotifyPropertyClass
{
private string col1;
public string Col1
{
get { return col1; }
set { col1 = value; OnPropertyChanged("Col1"); }
}
private List<string> col2;
public List<string> Col2
{
get { return col2; }
set { col2 = value; OnPropertyChanged("Col2"); }
}
}
public ObservableCollection<Test1> dc;
public MainWindow()
{
dc = new ObservableCollection<Test1>()
{
new Test1() { Col1 = "00" + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + "00",
Col2 = new List<string>() { "123", "456"} }
};
DataContext = dc;
InitializeComponent();
}
If you want to stretch the rows vertically you could use a converter that binds to the parent (inner) DataGrid
and sets the height of the DataGridRow
item containers based on the height of the DataGrid
:如果要垂直拉伸的行,你可以使用一个转换器结合到父(内)
DataGrid
并设置高度DataGridRow
基础上的高度项容器DataGrid
:
namespace WpfApplication1
{
public class RowHeightConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DataGrid dg = value as DataGrid;
if(dg != null && dg.Items.Count > 0)
{
return dg.ActualHeight / dg.Items.Count;
}
return 20; //return some default height
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
}
Sample usage:示例用法:
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" HeadersVisibility="None" CanUserAddRows="False"
xmlns:local="clr-namespace:WpfApplication1">
<DataGrid.Resources>
<local:RowHeightConverter x:Key="conv" />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Col1}" Width="*"/>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Path=Col2}" AutoGenerateColumns="False" HeadersVisibility="None" CanUserAddRows="False">
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<Setter Property="Height" Value="{Binding Path=., RelativeSource={RelativeSource AncestorType=DataGrid},
Converter={StaticResource conv}}" />
</Style>
</DataGrid.ItemContainerStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.