简体   繁体   中英

Dynamic Column headers and Header count in Datagrid WPF

I am constructing a datagrid in my WPF application. ItemSource of the Datagrid is bounded to an IEnumerable collection. I donno the ViewModel of my project. When I bind my datagrid itemsource to the datagrid I get column headers and Row values on the fly.

I donno the headers. It might be anything. I need to display detailed information of the selected row in the datagrid in a grid.

To do this i need to bing SelectedItem.HeaderName to the textblock of the grid. But what the issue here is I donno the name of the header. So i can't simply hardcode SelectedItem.Headername .

And number of columns may differ respectively. So my detailed view should also dynamic number Header names with its respective value when a row of my datagrid is selected.

As of now i have harcoded and seen the result in my xaml like below. because for a particular file i know their respective column 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" />

Since i am a begginer i couldn't figure out how to do this. I will be glad if u try to help me. And suggest some concepts i need to read related to this dynamic column generation, Count, assigning dynamic column headers to other control in UI. Thanks in advance!

I have made a small project contains dynamic generation of the grid (TextBlock + TextBox clollection) depending on DataGrid headers and object properties that made your collection in DataGrid. Hope that's what you are looking for. you can donwload it from my SkyDrive

XAML :

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

        </StackPanel>
    </Grid>

I have set this in Loaded event handler :

 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);
                    }

Text class :

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;
            }
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM