Wpf 每行中列数不同的dispay数据矩阵
我需要在WPF应用程序中显示一个没有固定表格结构的“数据矩阵”,即 数据行1:aaa bb cccWpf 每行中列数不同的dispay数据矩阵,wpf,Wpf,我需要在WPF应用程序中显示一个没有固定表格结构的“数据矩阵”,即 数据行1:aaa bb ccc 数据行2:222$$DDD eeee ... 所有数据都在数组列表中 我需要显示这些数据,并允许用户滚动浏览 奇怪的数据结构来自文本平面文件,我无法更改它 我读了很多书,并尝试了列表视图和网格视图,但到目前为止,我只能得到: 1。动态列创建,但这不好,因为我对每一行都有不同的模式 2.将每行数据呈现为(分隔/格式化)文本。但这并不好,因为所有行数据都在单个单元格中结束(显然)。 3.(还没有这样
数据行2:222$$DDD eeee
...
所有数据都在数组列表中 我需要显示这些数据,并允许用户滚动浏览
奇怪的数据结构来自文本平面文件,我无法更改它
我读了很多书,并尝试了列表视图和网格视图,但到目前为止,我只能得到:
1。动态列创建,但这不好,因为我对每一行都有不同的模式
2.将每行数据呈现为(分隔/格式化)文本。但这并不好,因为所有行数据都在单个单元格中结束(显然)。
3.(还没有这样做,希望避免)有很多带有触发器的数据模板,每个模板都有不同的列计数(比如从5到40),以适应绝大多数行类型 我的问题是:如何将此数据绑定到类似列表/网格的可滚动视图
提前感谢如果您不需要选择项目的功能,可以使用嵌套 只需将第一个
ItemsControl
绑定到数组集合,然后将itemstemplate
设置到另一个绑定到值数组的ItemsControl
大概是这样的:
<ScrollViewer Height="300" Width="400">
<ItemsControl ItemsSource="{Binding ListOfArrays}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding }">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding }" Width="50" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
如果您不需要选择项目,可以使用嵌套 只需将第一个
ItemsControl
绑定到数组集合,然后将itemstemplate
设置到另一个绑定到值数组的ItemsControl
大概是这样的:
<ScrollViewer Height="300" Width="400">
<ItemsControl ItemsSource="{Binding ListOfArrays}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding }">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding }" Width="50" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
一次查找最大列数。构建一个具有属性cols字符串[maxColCount]的简单行类。
在代码绑定中为GridView构建maxColCount列,并将源代码绑定到cols[x] 编译器需要一个属性,但您可以将列绑定到集合名称索引
用于构建和绑定GridViewColumn的语法
gvc = new GridViewColumn();
gvch = new GridViewColumnHeader();
gvch.Content = fd.FieldDef.DispName;
gvch.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch;
if (fd.FieldDef.Sort)
{
gvch.Click += new RoutedEventHandler(SortClick);
gvch.Tag = fd.FieldDef.Name;
}
// if (fd.ID == 0 || fd.ID == 1) gvc.Width = 60; sID, sParID
if (!fd.AppliedDispGrid) gvc.Width = 0;
gvc.Header = gvch;
gvBinding = new Binding();
gvBinding.Mode = BindingMode.OneWay;
gvBinding.Path = new PropertyPath("DocFields[" + sDocBaseResultDocsFieldsIndex.ToString() + "].DispValueShort");
template = new DataTemplate();
textblock = new FrameworkElementFactory(typeof(TextBlock));
textblock.SetValue(TextBlock.TextProperty, gvBinding);
textblock.SetValue(TextBlock.TextTrimmingProperty, TextTrimming.WordEllipsis);
// <Setter Property="TextTrimming" Value="WordEllipsis" />
template.VisualTree = new FrameworkElementFactory(typeof(Grid));
template.VisualTree.AppendChild(textblock);
gvc.CellTemplate = template;
gvSearchResults.Columns.Add(gvc);
gvc=newGridViewColumn();
gvch=新的GridViewColumnHeader();
gvch.Content=fd.FieldDef.DispName;
gvch.HorizontalContentAlignment=System.Windows.HorizontalAlignment.Stretch;
if(fd.FieldDef.Sort)
{
gvch.Click+=新建RoutedEventHandler(排序单击);
gvch.Tag=fd.FieldDef.Name;
}
//如果(fd.ID==0 | | fd.ID==1)全球价值链宽度=60;希德,斯巴德
如果(!fd.AppliedDispGrid)gvc.Width=0;
gvc.收割台=gvch;
gvBinding=新绑定();
gvBinding.Mode=BindingMode.OneWay;
gvBinding.Path=newpropertyPath(“DocFields[”+sdocbaseresultdocfieldsindex.ToString()+“].DispValueShort”);
模板=新的数据模板();
textblock=新FrameworkElementFactory(typeof(textblock));
设置值(textblock.TextProperty,gvBinding);
设置值(textblock.TextTrimmingProperty,TextTrimming.WordEllipsis);
//
template.VisualTree=新FrameworkElementFactory(类型(网格));
template.VisualTree.AppendChild(textblock);
gvc.CellTemplate=模板;
gvSearchResults.Columns.Add(gvc);
一次查找最大列数。构建一个具有属性cols字符串[maxColCount]的简单行类。
在代码绑定中为GridView构建maxColCount列,并将源代码绑定到cols[x] 编译器需要一个属性,但您可以将列绑定到集合名称索引
用于构建和绑定GridViewColumn的语法
gvc = new GridViewColumn();
gvch = new GridViewColumnHeader();
gvch.Content = fd.FieldDef.DispName;
gvch.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch;
if (fd.FieldDef.Sort)
{
gvch.Click += new RoutedEventHandler(SortClick);
gvch.Tag = fd.FieldDef.Name;
}
// if (fd.ID == 0 || fd.ID == 1) gvc.Width = 60; sID, sParID
if (!fd.AppliedDispGrid) gvc.Width = 0;
gvc.Header = gvch;
gvBinding = new Binding();
gvBinding.Mode = BindingMode.OneWay;
gvBinding.Path = new PropertyPath("DocFields[" + sDocBaseResultDocsFieldsIndex.ToString() + "].DispValueShort");
template = new DataTemplate();
textblock = new FrameworkElementFactory(typeof(TextBlock));
textblock.SetValue(TextBlock.TextProperty, gvBinding);
textblock.SetValue(TextBlock.TextTrimmingProperty, TextTrimming.WordEllipsis);
// <Setter Property="TextTrimming" Value="WordEllipsis" />
template.VisualTree = new FrameworkElementFactory(typeof(Grid));
template.VisualTree.AppendChild(textblock);
gvc.CellTemplate = template;
gvSearchResults.Columns.Add(gvc);
gvc=newGridViewColumn();
gvch=新的GridViewColumnHeader();
gvch.Content=fd.FieldDef.DispName;
gvch.HorizontalContentAlignment=System.Windows.HorizontalAlignment.Stretch;
if(fd.FieldDef.Sort)
{
gvch.Click+=新建RoutedEventHandler(排序单击);
gvch.Tag=fd.FieldDef.Name;
}
//如果(fd.ID==0 | | fd.ID==1)全球价值链宽度=60;希德,斯巴德
如果(!fd.AppliedDispGrid)gvc.Width=0;
gvc.收割台=gvch;
gvBinding=新绑定();
gvBinding.Mode=BindingMode.OneWay;
gvBinding.Path=newpropertyPath(“DocFields[”+sdocbaseresultdocfieldsindex.ToString()+“].DispValueShort”);
模板=新的数据模板();
textblock=新FrameworkElementFactory(typeof(textblock));
设置值(textblock.TextProperty,gvBinding);
设置值(textblock.TextTrimmingProperty,TextTrimming.WordEllipsis);
//
template.VisualTree=新FrameworkElementFactory(类型(网格));
template.VisualTree.AppendChild(textblock);
gvc.CellTemplate=模板;
gvSearchResults.Columns.Add(gvc);
1次查找最大列数。构建一个具有属性cols string[maxColCount]的简单行类。在代码绑定和绑定中为GridView构建maxColCount列