我正在尝试在WPF中创建一个datagrid,其中两个collumns是静态的,之后根据MVVM模式的参数列表的长度,collumns将是动态的。我们的想法是,您可以在第二列中选择带有下拉框的分布类型,然后它将生成参数。每行可以具有不同大小的参数,在选择行时,我将相应地改变标题参数以适合参数。我不知道如何实现这一点,任何人都可以指出我正确的方向。
澄清:行应该像数据网格一样,并且用户可以更改。单独数组的每个列应该有自己的标题。
目前的布局:
主窗口的XAML
<Window x:Class="WPF_Tester.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_Tester"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="10">
<DataGrid Name="data" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Distribution Type" Binding="{Binding Dist.Type}" />
<DataGridTextColumn Header="Param 1" Binding="{Binding Dist.Param1}" />
<DataGridTextColumn Header="Param 2" Binding="{Binding Dist.Param2}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
示例应用程序的代码:
using System.Collections.Generic;
using System.Windows;
namespace WPF_Tester
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Parameter> Data = new List<Parameter>();
Data.Add(new Parameter() { Id = 1, Name = "John Doe", Dist = new Distribution("static", 1, 0) });
Data.Add(new Parameter() { Id = 2, Name = "Jane Doe", Dist = new Distribution("guassian", 1, 2) });
Data.Add(new Parameter() { Id = 3, Name = "Sammy Doe", Dist = new Distribution("gaussian", 1, 2) });
data.ItemsSource = Data;
}
//Class with the parameters per row of the datagrid
public class Parameter
{
public int Id { get; set; }
public string Name { get; set; }
public Distribution Dist { get; set; }
}
//Distribution class
//PROBLEM: Parameters should be a list that can change in size depending on the selected type
public class Distribution
{
public Distribution(string type, int param1, int param2)
{
Type = type;
Param1 = param1;
Param2 = param2;
//This is what I would want to work, fill the datagrid with a list of changing size
//depending on the type of the distribution.
if (type == "static")
ParamList = new List<int> { 1 }; //This with what I would want to fill the datagrid
else if (type == "guassian")
ParamList = new List<int> { 1, 2 };
}
public string Type { get; set; }
public List<int> ParamList { get; set; }
public int Param1 { get; set; }
public int Param2 { get; set; }
}
}
}
基本问题:如何将每行不同大小的列表动态链接到数据网格。
答案 0 :(得分:1)
你真的需要数据横向移动。在您的情况下,我会使用TemplateColumn
DataGrid
并将其模板化为WrapPanel
,如下例所示:
<DataGridTemplateColumn Header="Parameters">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ItemsControl Margin="3" ItemsSource="{Binding ParametersList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="3" BorderBrush="Black" CornerRadius="5" Margin="3,0" MinWidth="100" >
<Grid>
<TextBlock>Here you will put whatever you wish to show... </TextBlock>
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>