Hi,
Please find below code to generate one property from Code Behind and Please give solution of following questions.
Requirement:
- Need to create it from Code Behind(Dynamically using propGrid.Properties.Add(...) method)
- Combo Box must contain key-value pair. Of which "Value" part is seen in combo box & key part will be used to indentify that combobox's item.
- need to add/remove item into/from combobox Dynamically.
- need to select perticular item by key or value part.
Q - 1: How can I change selection of combo-items on button click event? (ex. it defaults to "1" when loaded and I want to change it "3" when button is pressed.)
Q - 2: How can I remove/add item from/to combobox on button click event?
Code:
PropertyGridPropertyItem item = new PropertyGridPropertyItem();
item.DisplayName = "Combo Property";
item.Description = "Description of combobox type";
item.Category = "General Properties";
DataTemplate dataTemplate = new DataTemplate(); // DataTemple for Combo box
FrameworkElementFactory cmbBox = new FrameworkElementFactory(typeof(ComboBox));
cmbBox.Name = "myComboBox";
List<string> cb = new List<string>();
cb.Add("1");
cb.Add("2");
cb.Add("3");
cmbBox.SetValue(ComboBox.ItemsSourceProperty, cb); // Item source List<string>
cmbBox.SetValue(ComboBox.SelectedValueProperty, "1"); // Default value - 1
dataTemplate.VisualTree = cmbBox;
item.ValueTemplate = dataTemplate;
propGrid.Properties.Add(item as IPropertyDataAccessor); // Add it.
// Test Button Click event handler
private void btnTest_Click(object sender, RoutedEventArgs e)
{
// 0 index item
PropertyGridPropertyItem item = (PropertyGridPropertyItem)propGrid.Properties[0];
if (item != null)
{
FrameworkElementFactory cmbBox = item.ValueTemplate.VisualTree;
//Error:After a 'FrameworkElementFactory' is in use(sealed), it cannot be modified.
cmbBox.SetValue(ComboBox.SelectedItemProperty, 0);
}
}
Please find below code to generate one property from Code Behind and Please give solution of following questions.
Requirement:
- Need to create it from Code Behind(Dynamically using propGrid.Properties.Add(...) method)
- Combo Box must contain key-value pair. Of which "Value" part is seen in combo box & key part will be used to indentify that combobox's item.
- need to add/remove item into/from combobox Dynamically.
- need to select perticular item by key or value part.
Q - 1: How can I change selection of combo-items on button click event? (ex. it defaults to "1" when loaded and I want to change it "3" when button is pressed.)
Q - 2: How can I remove/add item from/to combobox on button click event?
Code:
PropertyGridPropertyItem item = new PropertyGridPropertyItem();
item.DisplayName = "Combo Property";
item.Description = "Description of combobox type";
item.Category = "General Properties";
DataTemplate dataTemplate = new DataTemplate(); // DataTemple for Combo box
FrameworkElementFactory cmbBox = new FrameworkElementFactory(typeof(ComboBox));
cmbBox.Name = "myComboBox";
List<string> cb = new List<string>();
cb.Add("1");
cb.Add("2");
cb.Add("3");
cmbBox.SetValue(ComboBox.ItemsSourceProperty, cb); // Item source List<string>
cmbBox.SetValue(ComboBox.SelectedValueProperty, "1"); // Default value - 1
dataTemplate.VisualTree = cmbBox;
item.ValueTemplate = dataTemplate;
propGrid.Properties.Add(item as IPropertyDataAccessor); // Add it.
// Test Button Click event handler
private void btnTest_Click(object sender, RoutedEventArgs e)
{
// 0 index item
PropertyGridPropertyItem item = (PropertyGridPropertyItem)propGrid.Properties[0];
if (item != null)
{
FrameworkElementFactory cmbBox = item.ValueTemplate.VisualTree;
//Error:After a 'FrameworkElementFactory' is in use(sealed), it cannot be modified.
cmbBox.SetValue(ComboBox.SelectedItemProperty, 0);
}
}