Combo Box in DataGrid WPF - wpf

I wanted a combo box in data grid which i added using the following code behind
DataColumn dc;
DataGridNS.DataGridTemplateColumn dgc = new DataGridNS.DataGridTemplateColumn();
dgc.Header = dc.ColumnName;
dgc.Width = 100;
dgc.IsReadOnly = true;
DataTemplate dtm = new DataTemplate();
FrameworkElementFactory outer = new FrameworkElementFactory(typeof(ComboBox));
dtm.VisualTree = outer;
dgc.CellTemplate = dtm;
dtgrdAtlas.Columns.Add(dgc);
i want to bind an array to this combo box.
how do i do that.
This code is in a seperate function where i add columns to data grid and my string array is in seperate function/class.

To bind a list to a ComboBox you need to set the ItemsSource, or Binding a List to the ItemsSource.
Here in this example I set directly the ItemsSource :
MyCombobox.ItemsSource = new List<int> {2, 3, 4 ,5, 6};

Related

Why I cannot set datagrid cell value if this column's header contains a dot?

My wpf app xaml is simple, only a datagrid:
<DataGrid ItemsSource="{Binding UserCollection}"/>
In code behind, UserCollection is a DataTable:
private DataTable userCollection;
public DataTable UserCollection
{
get { return this.userCollection; }
set
{
this.userCollection = value;
NotifyPropertyChanged();
}
}
In ViewModel.cs, code is simple, create 2 columns and add a row:
this.UserCollection = new DataTable();
UserCollection.Columns.Add("col1", typeof(int));
UserCollection.Columns.Add("co.l2", typeof(int));
var r=UserCollection.NewRow();
r["col1"] = 1;
r["co.l2"] = 2;
UserCollection.Rows.Add(r);
But the 2nd column which hearder contains a dot cannot display the value in UI, you can see the picture.
Why?
I have found the solution, replace dot char to another char by using :
StringNameWithDot.Replace(".", "\x2024");
Wpf datatable column header cannot contain ".","/","[]", here is another link:
What is it about DataTable Column Names with dots that makes them unsuitable for WPF's DataGrid control?

WPF dynamic DatagridComboboxColumn

I dynamically create a DataGridComboboxColum in the code. This works fine, however when I select an item in the Combobox in the Grid it disappears after i leave the combobox.
Here is the code:
MyDataGrid.ItemsSource = ergList;
DataGridComboBoxColumn cb = new DataGridComboBoxColumn();
cb.ItemsSource = data
cb.Header = "Tag";
cb.DisplayMemberPath = "Tag";
MyDataGrid.Columns.Add(cb);
How can i fix this?
You need to bind the selected value in the ComboBox to a property of the item in your ergList:
MyDataGrid.ItemsSource = ergList;
DataGridComboBoxColumn cb = new DataGridComboBoxColumn();
cb.ItemsSource = data
b.Header = "Tag";
cb.DisplayMemberPath = "Tag";
cb.SelectedValueBinding = new Binding("SomePropertyOfAnItemInErgList");
MyDataGrid.Columns.Add(cb);
Make sure that the types of the items in the ComboBox and the property to hold the selected value match.

trying to add listbox inside listboxitem in WPF

I am trying to use listbox inside listboxitem in WPF
Here , icTodoList and ActivityListBox are two main Lists. i want to add same number of lists in ActivityListBox as icTodoList and further i want to add one listbox under each listViewItem
The problem that is coming with this code is that it only binds listboxitem not listbox under wrap panel.
please let me know where i am wrong
for (var i = 0; i < icTodoList.Items.Count; i++)
{
ListBox lb = new ListBox();
ListBoxItem lbi = new ListBoxItem();
lb.Name = "InnerActivityList";
lb.Height = 500;
lb.Width = 225;
lb.Background = new SolidColorBrush(Colors.Red);
lbi.Content = lb;
ActivityListBox.Items.Add(lbi);
}

Binding to dynamically created controls

I'm having trouble binding values to dynamically created controls. When a user loads a custom file and that file will have a unknown number of arguments. Arguments have a Group property that when grouped will dynamically add tabItems to a tabControl. I then loop through the arguments and add a label and, for now, a textbox to a grid inside the tabs. though i intend to use different controls depending on the arument type. I want to bind the argument Value property to the textbox. The tabs, labels and textboxes are added fine but, no value binding
He if my not yet re-factored solution so far;
myTab.Items.Clear();
var args = viewModel.Arguments;
var groups = args.GroupBy(arg => arg.Groups);
foreach (var group in groups)
{
TabItemExt tab = new TabItemExt();
tab.Header = group.Key;
Grid grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
int count = 0;
foreach (var argument in group)
{
RowDefinition newRow = new RowDefinition();
grid.RowDefinitions.Insert(count, newRow);
LabelTextBlock label = new LabelTextBlock();
label.Text = argument.DisplayName;
Grid.SetRow(label, count);
Grid.SetColumn(label, 0);
TextBox textBox = new TextBox();
var binding = new Binding();
binding.Source = viewModel.Arguments[argument.Name];
//binding.Source = argument
binding.Path = new PropertyPath("Value");
textBox.SetBinding(TextBlock.TextProperty, binding);
Grid.SetRow(textBox, count);
Grid.SetColumn(textBox, 1);
grid.Children.Add(label);
grid.Children.Add(textBox);
count += 1;
}
tab.Content = grid;
myTab.Items.Add(tab);
}
textBox.SetBinding(TextBlock.TextProperty, binding);
should have been
textBox.SetBinding(TextBox.TextProperty, binding);
just a little over dependent on intellisense.

Listbox ScrollIntoView problem wpf

I am creating a listbox with itemsource of type dataview. I want to scroll the listbox to praticular item which is not selected. I am using the following code for selected item.
Code:
Binding listbox:
DataView dv = newDt.DefaultView;
dv.Sort = "Count Desc";
lbResult.DataContext = dv;
To get the row based on id:
var selectResult = from mypro in albumDetails.ToTable().AsEnumerable() where mypro.Field<string>("ID")==search.ID select mypro;
if (lbResult.SelectedItem != null)
{
lbResult.ScrollIntoView(**lbResult.Items[0]**);
}
How to get the index if the row in the list box.
Geetha
I don't know what is the albumDetails object. Here is the code to get index from DataView.
DataRow row = dataview.Select("ID='" + search.ID + "'")[0];
int i= dataview.Rows.IndexOf(row);

Resources