开发者

Creating WPF ComboBox Controls Dynamically

I need to create a DataGrid dynamically in WPF. The grid 开发者_高级运维should be populate with the Rows and columns with dynamically generated controls (comboBox,TextBoxes). I need to populate the grid with Three coulmns and N-rows(the number of rows is based on the XML nodes available in a XML file). I have the DocumentProperties.XML in my WPF application as a Content.

    <DocumentProperties>
<Properties>
  <Property Name="DocumentName">
      <Id>1</Id>
      <Name>DocumentName</Name>
      <Type>LogicalOperator</Type>
      <Language>en-US</Language>
  </Property> 
   <Property Name="TemplateUsed">
      <Id>2</Id>
      <Name>TemplateUsed</Name>
      <Type>LogicalOperator</Type>
      <Language>en-US</Language>
  </Property> 
  ....
 </Properties>
<Operators>
<Category Type="LogicalOperator">
    <Operator value="Equal"></Operator>
    <Operator value="NotEqual"></Operator>
</Category>
<Category Type="TimeOperator">
    <Operator value="Greater"></Operator>
    <Operator value="Smaller"></Operator>
    <Operator value="GreaterOrEqual"></Operator>
    <Operator value="SmallerOrEqual"></Operator>
</Category>
</DocumentProperties>

In the Grid I need to create a ComboBox in Colum1 and Column2. The column1 combo will load the Name attribute values available in the XML. Based on the Type element value of the first comboBox selection, I need load the second combobox with the values in the Category Node. The 3r column in the grid should be poplate with either a textbox or Datepikcer based on the Type of the selected property. If the XML has 6 property Nodes then the maximum number of rows in the Grid should be <=6. The above content will be used for selecting properties for advanced search.

I have started with creating the grid from code behind. The part of the code I have tried is as follows:

documentPropertyCombo = new ComboBox[drivesCount];
                operatorCombo = new ComboBox[drivesCount];
                //Load the XML Document
                XmlDataProvider xdp = new XmlDataProvider();
                XmlDocument doc = new XmlDocument();
                doc.Load(".\\Data\\DocumentProperties.xml");
                xdp.Document = doc;
                xdp.XPath = "DocumentProperties/Properties/Property";
                xdp.Document = doc;

                drivesGrid.DataContext = xdp;
                var binding = new Binding("DocumentProperties/Properties/Property") { Source = xdp };
                binding.Mode = BindingMode.OneTime;

// Adding ComboBoxes and TextBoxes to Grid.
                for (int i = 0, j = 1; i < drivesCount; i++, j++)
                {
                    //Initialize ComboBox to show Document Property.
                    //documentPropertyCombo[i] = new ComboBox();
                    documentPropertyCombo[i] = new ComboBox(); //CreateComboBox(new Thickness(30, 101, 0, 0), i + j, 1);
                    documentPropertyCombo[i].Width = 101;
                    documentPropertyCombo[i].Height  = 30;
                    documentPropertyCombo[i].Visibility = Visibility.Visible;
                    //BindingOperations.SetBinding(myText, TextBlock.TextProperty, myBinding);
                    documentPropertyCombo[i].IsReadOnly = true;
                    documentPropertyCombo[i].DisplayMemberPath = "@Name";
                    BindingOperations.SetBinding(documentPropertyCombo[i], ComboBox.TextProperty, binding);

                    Grid.SetRow(documentPropertyCombo[i], i + j);
                    Grid.SetColumn(documentPropertyCombo[i], 1);
                    drivesGrid.Children.Add(documentPropertyCombo[i]);

 ...
}
LayoutRoot.Children.Add(drivesGrid);

The Controls are created dynamically but the data is not binded to the ComboBox. Am I missing anything in the above code? I assume I might need to add a DataTemplate for the ComboxBox dynamically. Can anybody verify the above code? Also I need to know how to add the events for the dynamically created comboxBox in Column1 and update the content of the ComboxBoxes in Column2 based on the selection in Combo1?


U said...

I need to create a DataGrid dynamically in WPF

Are you using the WPF datagrid control? Your sample code doesnt have it anywhere.

If you were using it then the data grid allows us to specify DataGridTextBoxColumn and DataGridComboBoxColumn along with XPath binding from XMLs.

I suggest you to use that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜