Setting the datasource of a WPF Grid - c#

I'm very new to WPF, and am trying to set the datasource (which the WPF Grid doesn't have as a property) of my grid to take a List. Does anyone have any code examples of how to do this. I have googled it, but can't find any really good examples.
(Oh, and can anyone suggest a good site for all round WPF Code examples?)
Thanks

If you're referring to a WPF Grid, you can't bind it to data; it's meant for layout purposes only; you might want to look into one of the controls that inherit from ItemsControl, such as ListView.
The property you'd bind your list to, is called ItemsSource.

The other control you might be thinking of is the GridView
There's also the DataGrid (Note old link) in the WPF Toolkit which implements a lot of the same functionality as the WinForms DataGridView

Related

ListView in Avalonia UI

Unfortunately Avalonia UI is still behind WPF and ListView is missing. I have found out one alternative, ItemsRepeater, but its very basic and not interactive. Apparently you can make it interactive, but I have to yet found an example that takes less than 200 lines of code and actually works. I just refuse to believe no one made something that works as simply as ListView in WPF.
I want to display List of custom data model (property A and B etc), and I'm really tempted to just use DataGrid. Will be ugly, but it will work.
You can try https://github.com/wieslawsoltes/DataBox it's very simple control with columns based on ListBox

XamDataChart with unknown number of series

I am using an Infragistics XamDataChart and want to bind a collection in my view model to the chart's Series property, since I don't know in advance how many line charts I will need to display.
From what I can gather from old posts in the Infragistics support forums, the Series property is read only and thus doesn't support binding directly. A solution is offered here but it seems like overkill for such a simple goal (maybe to me it just seems simple).
Has anyone here done any work with the Infragistics xamDataChart and MVVM? The ultimate goal is to be able to have a collection in my view model that contains a variable number of 'series' that I can just bind to the chart. Now I can probably do this if I just write some code behind for my xaml, access the DataContext (viewModel) and listen to the collection property, directly adding/removing series to the chart as necessary, but I was looking for a more MVVM way.
Thanks.
Since the Series collection of the XamDataChart is read-only, in order to be able to generate the Series dynamically,based on you VeiwModel, you should use helper class, similar to the approach that Graham Murray has suggested in the thead that you have referred. I have created a sample applicaiton, that show how you can create similar appraoch for binding the series of the XamDataChart to collection of your ViewModel. You can download the sample from here:
http://users.infragistics.com/Samples/SeriesBinder.zip
Sincerely,
Krasimir

WPF - Display db records into a list

I am quite new to WPF and XAML but my background is of ASP.NET and C# so I have a vague idea of how it works.
In .net, I can use the repeater, datalist, gridview, bind to them a DataTable and output from that DataTable. I am now wanting to do the same with WPF.
Basically, I want to display simple records from a database (preferably using a DataTable as I usually work with those). The list might be something like this with two columns
1) Grey TV
2) Red car
3) Blue motorbike
I have looked around but I can't get a definitive answer on what control to use. Some people say ItemsControl and some people say DataGrid. Can anyone help me here?
Thanks in advance.
A DataGrid is used for displaying Table-like data (Per record multiple columns). An ItemsControl is used to display data using your own ItemTemplate, in which you are unlimited in how to represent the items and in what directions or alignments.
Another good useable control for you might be a ListView, which works just as a ListBox except it doesn't have any selection logic. And you can choose between four different ways of displaying your items using the View property (http://msdn.microsoft.com/en-us/library/system.windows.forms.view.aspx).
In your case I would suggest using a ListView.
To bind any items to the control, you have to set the DataContext on the UserControl or the Control itself. And then bind the ItemsSource property to a local List or Collection using the Binding markup extension (http://msdn.microsoft.com/en-us/library/ms750413.aspx). To learn more about data-binding go here:
http://msdn.microsoft.com/en-us/library/ms752347.aspx

Share listbox across controls/pivot items on WP7

I was wondering if there was a way to share a listbox on the same page.
Basically, I have a listbox that is bound to a collectionviewsource. I also have 5-6 pivot controls that will use the same listbox, but will filter the collectionviewsource differently.
I was wondering if it was possible to say have a listbox as a resource and display it on all hte pivots then when the pivot changes, I can apply a different filter to the collectionviewsource.
Can anyone steer me the right way?
Thanks!
You could create an ItemTemplate for the Pivot which includes the listbox.
I find it easier to maintain this scenario by explicilty creating each pivotItem in XAML but having all teh listboxes share the same ItemTemplate though. I find it easier to see what's going on by just looking and it also makes it easier if you ever need to change the template of one of the listboxes. (I find this seems to happen quite often.)

How do I access the controls in a WPF DataGrid

In good old (well!!) WinForms days the datagrids row used to the be the actual control and you could then access the DataItem.
In WPF its all flipped and dataGrid.Items is just the source data.
I am probably doing this the wrong way round as im a bit of a WPF newb but how can I iterate through the rows of my gridview grabbing the values from certain labels, textboxes etc?
Yes, you are doing this the wrong way round. What you should be doing is iterating through the items in your data source - it's where all the values are, after all.
It's possible to iterate through the WPF objects, but it's not trivial. And there's a significant problem that you'll run into if you try.
You can use the VisualTreeHelper class to search the visual tree and find the DataGrid's descendant objects. If you play with this long enough, eventually you'll figure out how to find the specific controls you're looking for. But the DataGrid (actually, the VirtualizingStackPanel in its control template) virtualizes its visual children. If an item hasn't appeared on the screen yet, its WPF objects haven't been created yet, and you won't find them in the visual tree. You may not be able to find what you're looking for, not because you don't have a way of finding it, but because it doesn't exist.
If you're using value converters and formatting in your bindings (which is the only reason I can think of that you'd want to look at the WPF objects and not the underlying data items), I'm afraid the answer is: don't do that. Do the value conversion and formatting in your data source, and expose the results as properties that can be bound to directly.
It's certainly possible to use WPF without using the MVVM pattern. But this is the kind of brick wall that you can run into if you don't.
You can use this
public DataGridRow TryFindRow(object item, DataGrid grid)
{
// Does not de-virtualize cells
DataGridRow row = (DataGridRow)(grid as ItemsControl).ItemContainerGenerator.ContainerFromItem(item);
return row;
}
where item represent the data displayed on the row.
Hope this helps.

Categories

Resources