如何将Row datagrid的值设置为另一个datagrid的头?

时间:2016-05-26 14:39:11

标签: wpf

我想在更改datagrid2下面的Rows列时更改datagrid1的标题。

它是生成Automaticaly的DataGrid2列的代码。 如何将c1列的行绑定到datagrid 1的标题?

 public class DataGridStructure
    {

        public string NameField { get; set; }
        public string HeaderText { get; set; }

    }
    public List<DataGridStructure> CreateDataTable()
    {

        List<DataGridStructure> dgs = new List<DataGridStructure>();
        dgs.Add(new DataGridStructure() { NameField ="name field1" , HeaderText = "Header1" });
        dgs.Add(new DataGridStructure() { NameField = "name field2", HeaderText = "Header2" });
        dgs.Add(new DataGridStructure() { NameField = "name field3", HeaderText = "Header3" });
        dgs.Add(new DataGridStructure() { NameField = "name field4", HeaderText = "Header4" });  
         return dgs;

     }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        DataGridTextColumn c1 = new DataGridTextColumn();
        c1.Header = "name Field";
        c1.Binding = new Binding("NameField");
        dataGrid2.Columns.Add(c1);

            DataGridTextColumn c2 = new DataGridTextColumn();
        c2.Header = ""Header Text;
        c2.Binding = new Binding("HeaderText");
        c2.IsReadOnly = true;
        dataGrid2.Columns.Add(c2);
 dataGrid2.ItemsSource = CreateDataTable();
    }

绑定到DataBase的datagrid1的设计代码:

<DataGrid

                                          AutoGenerateColumns="False"
                                          RowHeaderWidth="30" Name="datagrid1"
                                          VerticalAlignment="Top" 
                                          FlowDirection="RightToLeft"
                                          LoadingRow="DataGrid_LoadingRow"
                                          IsReadOnly="True" >
                                       <DataGrid.Columns>
                                            <DataGridTextColumn  Width="100"  Binding="{Binding Path=Turn}"   FontSize="14" FontWeight="bold" />
                                            <DataGridTextColumn  Width="100"  Binding="{Binding Path=Name}"   FontSize="14" FontWeight="bold" />
                                            <DataGridTextColumn   Width="100"  Binding="{Binding Path=Date}"   FontSize="14" FontWeight="bold"/>                                              
                                            <DataGridTextColumn  Width="100"  Binding="{Binding Path=Amount}" FontSize="14" FontWeight="bold" />                    
                                       </DataGrid.Columns>
                                    </DataGrid>

如果需要更多信息,请告诉我,谢谢。

0 个答案:

没有答案