Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将WPF中每行大小不同的列表动态链接到datagrid_C#_Wpf_Datagrid - Fatal编程技术网

C# 如何将WPF中每行大小不同的列表动态链接到datagrid

C# 如何将WPF中每行大小不同的列表动态链接到datagrid,c#,wpf,datagrid,C#,Wpf,Datagrid,我试图在WPF中创建一个datagrid,其中两列是静态的,之后这些列将根据基于MVVM模式的参数列表的长度而动态。其思想是,您可以在第二列中使用下拉框选择分发类型,然后它将生成参数。每行可以有不同大小的参数,在选择行后,我将相应地更改标题参数以适应参数。我不知道如何做到这一点,有人能给我指出正确的方向吗 说明:行的行为应该与数据网格一样,并且可以由用户更改。单独数组的每一列都应该有自己的标题 当前布局: 主窗口的XAML <Window x:Class="WPF_Tester.Main

我试图在WPF中创建一个datagrid,其中两列是静态的,之后这些列将根据基于MVVM模式的参数列表的长度而动态。其思想是,您可以在第二列中使用下拉框选择分发类型,然后它将生成参数。每行可以有不同大小的参数,在选择行后,我将相应地更改标题参数以适应参数。我不知道如何做到这一点,有人能给我指出正确的方向吗

说明:行的行为应该与数据网格一样,并且可以由用户更改。单独数组的每一列都应该有自己的标题

当前布局:

主窗口的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; }
        }
    }
}
使用System.Collections.Generic;
使用System.Windows;
名称空间WPF_测试仪
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
列表数据=新列表();
Add(新参数(){Id=1,Name=“John Doe”,Dist=new Distribution(“static”,1,0)});
Add(新参数(){Id=2,Name=“Jane Doe”,Dist=new Distribution(“guassian”,1,2)});
Add(新参数(){Id=3,Name=“Sammy Doe”,Dist=新分布(“gaussian”,1,2)});
data.ItemsSource=数据;
}
//使用datagrid每行的参数初始化
公共类参数
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共分发区{get;set;}
}
//分布类
//问题:参数应该是一个列表,其大小可以根据所选类型而变化
公共阶级分配
{
公共分发(字符串类型,int-param1,int-param2)
{
类型=类型;
Param1=Param1;
Param2=Param2;
//这就是我想要做的,用一个改变大小的列表填充datagrid
//取决于分发的类型。
如果(类型==“静态”)
ParamList=new List{1};//这是我想要填充数据网格的内容
else if(类型=“guassian”)
ParamList=新列表{1,2};
}
公共字符串类型{get;set;}
公共列表参数列表{get;set;}
公共int参数1{get;set;}
公共int参数2{get;set;}
}
}
}

基本问题:如何将每行大小不同的列表动态链接到数据网格。

您确实需要将数据横向移动。在您的情况下,我将使用
DataGrid
TemplateColumn
,并将其模板化为
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>

在这里,你会把你想展示的东西放在这里。。。

尽管这是可行的,但这并不完全是我想要的,我希望它的行为与网格一样,每个值都有一个单独的标题。另外,我不知道如何将参数列表中的数据动态链接到TextBlock。如果我使用代码,文本块的内容只会根据列表中的项目数量重复,而不会实际显示项目本身。