WPF window inserting an array into DataGrid - wpf

WPF window in C# doesn't have option of Inserting something into DataGrid immediately, like as WinForms Form
DataGridView.Rows.Add(whatever)
What is alternative for this code 👆?
So how can I insert an array into DataGridView in WPF window?

You can actually add objects directly to the Items property of the DataGrid:
dataGrid.Items.Add(whatever);
But if you want to be able to edit the items, you should set or bind the ItemsSource property to an IList:
dataGrid.ItemsSource = new List<object> { whatever };

You need to bind the DataGrid to an ObservableCollection. If you insert into the ObservableCollection, it'll appear on the UI

Related

Selecteditem event in MVVM silverlight

i have a datagrid bound to a property. In this grid i have columns which consists of cells which are like hyperlink i mean when user clicks on the cell value based on these values another gird will get populated. i want to know how to get the cell value and pass it to some method so that other grid will get populated.
The best way to do this is in your viewmodel.
You should bind the SelectedItem of your datagrid to a new property in your ViewModel. In the set method of this new Property, call a new method to populate a new ObservableCollection/List/whatever...
Finally, bind your "other grid" ItemsSource to this new observable collection from your ViewModel.
Edit:
If you need to load one thing or another depending on the column you are going to use the code behind, take a look at this:
Silverlight DataGrid how to get cell value from a selected item?

Silverlight DataGrid and Binding columns

I have a silverlight datagrid control and columns autogenerate property is set to false.
I am using MVVM and wants to bind the columns collection.
The data which i get is from xml. Something similar to sample code
Link
Now by passing the datagrid control from xmal file to the modelview I can get the expected behavior but with that way , i am adding columns in the datagrid control.
Is there any way, so that I can bind the columns collection with the datagrid control so that no need to pass the control to view model.
-Rajesh
Sounds like a strange implementation of MVVM you have there. Your ViewModel should be completely independent of the View. If you want to create dynamic columns on the grid then why not expose a relevant property collection on the ViewModel, and iterate through it in a relevant method on the View code behind e.g.
//Used with an Infragistics XamWebGrid control
private void BuildGrid() {
foreach (var dataItem in **ViewModel**.MyDataCollection)
{
var myCol = new TemplateColumn
{
HeaderText = dataItem.ItemNm,
Key = dataItem.PrimaryKey
};
MyGrid.Columns.Add(myCol);
}
}
You really shouldn't be passing controls from the View over to the ViewModel. The ViewModel should have no knowledge of any controls on the View.

WPF Accessing Items inside Listbox which has a UserControl as ItemTemplate

I have Window that has a ListBox
ListBox(MyListBox) has a DataTable for its DataContext
ListBox's ItemSource is : {Binding}
Listbox has a UserControl(MyUserControl) as DataTemplate
UserControl has RadioButtons and TextBoxes (At first They're filled with values from DataTable and then user can change them)
Window has one Submit Button
What I want to do is, when user clicks the submit button
For each ListBox Item, get the values form UserControl's TextBoxes and RadioButtons.
I was using that method for this job :
foreach(var element in MyListBox.Items)
{
var border = MyListBox.ItemContainerGenerator.ContainerFromItem(element)as FrameworkElement;
MyUserControl currentControl = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(myBorder,0) as Border,0)as ContentPresenter,0)as MyUserControl;
//And use currentControl
}
I realised nothing when using 3-5 items in Listbox. But when I used much more items, I saw that "var border" gets "null" after some elements looped in foreach function.
I found the reason here :
ListView.ItemContainerGenerator.ContainerFromItem(item) return null after 20 items
So what can I do now? I want to access all items and get their values sitting on user controls.
Thanks
You should use objects who implement INotifyPropertyChanged and bind an ObservableCollection of it to the ItemSource
And then you can get all the list of items.
Here some quick links from MSDN to get more informations
How to: Implement Property Change Notification
Binding Sources Overview
You should google for some tutorials about this.
Zied's post is a solution for this problem. But I did the following for my project:
I realised that there's no need to use UserControl as DataTemplate in my project. So I removed ListBox's DataTemplate.
I removed MyListBox.DataContext = myDataTable and used this:
foreach(DataRow dr in myDataTable.Rows)
{
MyUserControl muc = new MyUserControl(dr);
myListBox.Items.Add(muc);
}
I took DataRow in my UserControl's constructor and did what I want.
And at last I could access my UserControls in ListBox by using :
foreach(MyUserControl muc in
myListBox)
{
//do what you want
}
Easy huh? :)

How do I loop through a set of items in a databound Silverlight control such as the ListBox?

I have a silverlight ListBox that has it's ItemsSource set. From the C# code behind I want to loop through each ListBox item and change the value of the text and access controls that are in the ListBox ItemTemplate. How would I do that?
ListBox controls in silverlight are bound to an ienumerable type, so that if any value in the ListBox changes, the underlying data is changed and vice versa depending on what type of binding you require. To effectively iterate the items you'll want to iterate through the enumerable object you've bound to.
You can get to the collection by accessing the ListBox.ItemsSource property and change the text of appropriate items, perform LINQqueries etc. If you have bound the controls correctly, saving the collection should update the list.
Hope this helps!

WPF ListBox insert

I have a WPF Listbox defined in a DataTemplate that has its ItemsSource bound to an ObservableCollection. When new items are added to the collection, I want the ListBox to work in "insert mode", always adding to the front of the list, not the default append mode.
How do I make the ListBox use the insert mode when not adding to it directly in code?
Use the Insert method of the ObservableCollection and pass the index 0 - that will add it to the beginning of the collection.

Resources