首页 文章

Datagrid WPF中的动态列 Headers 和标头计数

提问于
浏览
0

我正在我的WPF应用程序中构建一个数据网格 . Datagrid的 ItemSource 被绑定到 IEnumerable 集合 . 我不知道我的项目的ViewModel . 当我将datagrid itemsource绑定到datagrid时,我会动态获取列 Headers 和Row值 .

我不知道 Headers . 它可能是任何东西 . 我需要在网格中显示数据网格中所选行的详细信息 .

为此,我需要将SelectedItem.HeaderName绑定到网格的文本块 . 但这里的问题是我不知道 Headers 的名称 . 所以我不能简单地硬编码 SelectedItem.Headername .

列数可能分别不同 . 因此,当我选择一行数据网格时,我的详细视图也应该是动态数字 Headers 名称及其各自的值 .

截至目前,我已经在我的xaml中进行了编码并看到了如下结果 . 因为对于特定的文件,我知道它们各自的列 Headers ,

<Label HorizontalAlignment="Stretch"
       VerticalAlignment="Center"
       Grid.Row="0"
       Grid.Column="0">Header2:
</Label>

<TextBlock Grid.Row="0"
           Grid.Column="1"
           Name="Header2"
           Text="{Binding SelectedItem.date,  ElementName=dataGrid1}"
           Width="auto"
           Height="auto"
           HorizontalAlignment="Stretch"
           VerticalAlignment="Center" />

<Label Grid.Row="0"
       Grid.Column="2"
       VerticalAlignment="Center">Header3:</Label>

<TextBlock Grid.Row="0"
           Grid.Column="3"
           Name="username"
           Text="{Binding SelectedItem.Header3, ElementName=dataGrid1}"
           Width="auto"
           Height="auto"
           HorizontalAlignment="Stretch"
           VerticalAlignment="Center" />

<Label Grid.Row="0"
       Grid.Column="4"
       VerticalAlignment="Center">Header4:</Label>

<TextBlock Grid.Row="0"
           Grid.Column="5"
           Name="level"
           Text="{Binding SelectedItem.header4, ElementName=dataGrid1}"
           Width="auto"
           Height="auto"
           HorizontalAlignment="Stretch"
           VerticalAlignment="Center" />

<Label Grid.Row="1"
       Grid.Column="0"
       VerticalAlignment="Center">Header5:</Label>

<TextBlock Grid.Row="1"
           Grid.Column="1"
           Name="logger"
           Text="{Binding SelectedItem.header5, ElementName=dataGrid1}"
           Width="auto"
           Height="auto"
           HorizontalAlignment="Stretch"
           VerticalAlignment="Center" />

<Label Grid.Row="1"
       Grid.Column="2"
       VerticalAlignment="Center">Headr6:</Label>

<TextBlock Grid.Row="1"
           Grid.Column="3"
           Name="thread"
           Text="{Binding SelectedItem.header6, ElementName=dataGrid1}"
           Width="auto"
           Height="auto"
           HorizontalAlignment="Stretch"
           VerticalAlignment="Center" />

由于我是一个乞丐,我无法弄清楚如何做到这一点 . 如果你想帮助我,我会很高兴的 . 并建议我需要阅读的一些与此动态列生成相关的概念,Count,将动态列 Headers 分配给UI中的其他控件 . 提前致谢!

1 回答

  • 0

    我创建了一个包含动态生成网格(TextBlock TextBox clollection)的小项目,具体取决于DataGrid标头和在DataGrid中创建集合的对象属性 . 希望这就是你要找的东西 . 你可以从my SkyDrive下载它

    XAML:

    <Grid>
            <StackPanel>
            <StackPanel x:Name="myStackPanel" Orientation="Vertical"></StackPanel>
                <DataGrid x:Name="myDataGrid" ItemsSource="{Binding MySource}" AutoGenerateColumns="True">
            </DataGrid>
    
            </StackPanel>
        </Grid>
    

    我在Loaded事件处理程序中设置了它:

    for (int i = 0; i < myDataGrid.Columns.Count; i++)
                        {
                            var childStackPanel = new StackPanel { Orientation = Orientation.Horizontal };
    
                            var myTextBlock = new TextBlock { Text = myDataGrid.Columns[i].Header + " : " };
    
                            var myTextBox = new TextBox { Width = 200 };
    
                            Type myType = typeof(Text);
                            IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
                            myTextBox.SetBinding(TextBox.TextProperty,
                                                  new Binding("SelectedItem." + props[i].Name) { ElementName = "myDataGrid" });
                            childStackPanel.Children.Add(myTextBlock);
                            childStackPanel.Children.Add(myTextBox);
                            myStackPanel.Children.Add(childStackPanel);
                        }
    

    文字课:

    public class TranslationText
        {
            private string _translation;
            private bool _isTranslated;
    
            public string Translation
            {
                get { return _translation; }
                set
                {
                    _translation = value;
                }
            }
    
            public bool IsTranslated
            {
                get { return _isTranslated; }
                set
                {
                    _isTranslated = value;
                }
            }
    
        }
    

相关问题