动态添加新的DataGrid

时间:2015-02-11 19:19:00

标签: c# xml wpf datagrid

我在TabItem上有DataGrid,在XAML文件中标识。

<TabItem Header="Новая вкладка">
      <TabItem.DataContext>
            <XmlDataProvider x:Name="XMLData" Source="/database/stroyka1.bas" XPath="JobArray/job"/>
      </TabItem.DataContext>
            <DataGrid 
             Name="JobsDataGrid" 
             ItemsSource="{Binding}"
             AutoGenerateColumns="false" Height="Auto" Width="Auto"
             IsReadOnly="False" CanUserAddRows="True">
                  <DataGrid.Columns>
                      <DataGridTextColumn Header="ID" Binding="{Binding XPath=id, Mode=TwoWay}" />
                      <DataGridTextColumn Header="Название" Binding="{Binding XPath=name, Mode=TwoWay}" />
                      <DataGridTextColumn Header="Цена за единицу" Binding="{Binding XPath=price, Mode=TwoWay}"/>
                       <DataGridTextColumn Header="Единица измерения" Binding="{Binding XPath=measure, Mode=TwoWay}"/>
                        </DataGrid.Columns>
             </DataGrid>
      </TabItem>    

这很好用,我在这个DataGrid中找到add new rows的方法。但我需要同时使用DataGrids的更多标签。我试图在新的TabItem中创建新的DataGrid,但我看到了空行。 我的代码:

private void OpenBaseMenuItem_OnClick(object sender, RoutedEventArgs e)
    {
        var curTabOpFDial = new OpenFileDialog();  //open new XML-file
        curTabOpFDial.ShowDialog();
        var tempTabItem = new TabItem();       //create new TabItem in TabControl
        tempTabItem.Header = curTabOpFDial.SafeFileName;        //rename name of Tab

        var curTabDG = new DataGrid();       //create new DataGrid

        var curTabXmlDP = new XmlDataProvider();   //new XMLDataProvider

        var curTabUri = new Uri(curTabOpFDial.FileName); //edit xmldataprovider properties
        curTabXmlDP.Source = curTabUri;
        curTabXmlDP.XPath = "JobArray/job";

        tempTabItem.DataContext = curTabXmlDP;    //create new link with TabItem and XMLDataProvider

        DataGridTextColumn curTabDGTC = new DataGridTextColumn();   //creating 1 column for example
        curTabDGTC.Header = "ID";
        curTabDGTC.Binding = new Binding(){Source = curTabXmlDP, XPath = "id", Mode=BindingMode.TwoWay}; 

        curTabDG.Columns.Add(curTabDGTC);
        tempTabItem.Content = curTabDG;
        GeneralTabControl.Items.Add(tempTabItem);


    }

正如你所看到的 - 我试图移动所有的XAML,但它不起作用:(

0 个答案:

没有答案