ListView in Avalonia UI - c#

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

Related

WPF ListBox.SelectedItems as ListBoxItem

Is there a way to have the selected items of a ListBox as a ListBoxItem in the event SelectionChanged (on the ListBox) in WPF?
Right now, when I call listBox.SelectedItems I get the list of items in the format of my data source.
For example, when I create my ListBox I bind it to a CustomListBoxViewModel as a data source, so when I called SelectedItems I get a list of CustomListBoxViewModel objects.
Thanks!
EDIT:
The code that was given in the chosen answer worked perfectly for my use case.
On the other hand, I completely understand that this is a violation of the MVVM pattern. I'll use the code you provided in code behind of a xaml view.
The reason behind my original question was that I need to implement a ListBox that has some disabled ListBoxItems inside. Those items would have to be unselectable.
I tried to use an Attached Property IsSelectable on my ListBoxItems and it didn't work well. This attached property was greatly inspired by this post. The problem was that when I was directly selecting a disabled item, the attached property worked perfectly. The item couldn't be selected and it wasn't inside the SelectedItems collection. But if I wanted to select all the items by pressing CTRL+A, all the ListBoxItems were selected even the disabled ones and were found in the SelectedItems collection.
Whatever reason you're doing this for, it's probably a serious violation of MVVM that you'll bitterly regret for the rest of your life.
But the first step on the road to perdition is always an easy one.
Here's how:
var listBox = (ListBox)sender;
var selectedListBoxItems =
listBox.SelectedItems.Cast<Object>()
.Select(item => (ListBoxItem)listBox.ItemContainerGenerator.ContainerFromItem(item))
.ToList();
Just don't come crying to me when it all ends in tears.
No but seriously, there's are a few good reasons to do this kind of thing in WPF, but they're far less common than somebody new to WPF would expect. Pretty much any normal case is best handled by having your C# code interacting only with your data items, and do stuff to the ListBoxItems in XAML via styles and templating. Once you get used to that way of thinking, it's very powerful, flexible, productive, and maintainable. Codebehind is what you do for odd cases when all other reasonable avenues fail. Drag and drop, for example.

ListView with TreeViewItems in xaml

I'm pretty new to c#, the first thing that I'm trying to make is a ListView with data bindings which has turned out ok.
I'm now trying to make items have a twist button if the underlying model has any children (like the TreeView). Each of the children will have columns the same as all the top level items.
How would I go about doing this? Is there an already existing control like this? If not would I be better off dressing up a TreeView to look like a ListView, or dress up a ListView to look like a TreeView?
I went down the road outlined in this solution which dresses up a TreeView, but the end result looks pretty awful and the heading is actually just an item, so you lose all the nice column sizing and column buttons that can hook up to column sorting that you get in ListView so that route actually seems like it would be more work.
I noticed the new task manager has a control exactly like what I'm trying to create, I don't know how this made? probably in C though.
Microsoft provides a sample that appears to be what you are looking for. A write-up of the example can be found here:
http://msdn.microsoft.com/en-us/library/vstudio/ms771523(v=vs.90).aspx
When you build and run the example you will end up with something resembling this:
There is a large amount of templating done in the example, so you will be able to make things look the way you want.
What you describe sounds a bit like a TreeListView, and if you google 'WPF TreeListView' you will see some solutions that might be good for you. I have used one from Telerik, but it might be overkill depending on how complicated your needs are.
If you only want one sub-level like the image you attached, you might want to just roll your own using a ListView with a complex DataTemplate for the first column which would show an expander button and a simple ListBox bound to the children items.
Similar to the answer here, except your cell would have a checkbox styled to look like the arrow, the text for the item, and a child ListBox. Then bind the visibility of the child ListBox to the state of the checkbox.

Create a custom Data Grid from Silverlight

I'm watching at this page:
http://leeontech.wordpress.com/2010/02/01/summary-row-in-datagrid/
But they're using silverlight. I'm trying to create that user control to use it in a WPF C# application. I mean, not using Silverlight. But I can't find the namespaces: GroupHeader
I'm having a hard time with this. Thanks in advance.
Okay listen, you can totally do this, and in some scenarios I even recommend it.
Using a CollectionViewSource you can easily group your data. In the HeaderTemplate you can even use an Expander (or make your own) and get the animation you might be wanting. Here's a link to a sample of this: http://jerrytech.blogspot.com/2010/06/wpf-data-presentation-step-by-step.html
Using an ItemsControl, you can easily present your groups and details. In the ItemTemplate you can use styles make this look like a grid (if that is really what you want). You can also shift the style based on the type if your collection has more than one type of object in it (eat that datagrid!).
You can wire up your column headers (which will really be custom objects, right?) and handle all the sorting and stuff like that. They will look just right! Not like datagrid WinForm column headers!
Here's what's hard (not impossible, but more coding).
User-resizable columns.
User-rearrangable columns.
New record using bottom, empty row.
Paste from Excel (doesn't work right in datagrid either).
Select Row, highlight Column header.
That's it.
In lots of situations, this is really nice.
For the most part, I cannot stand the datagrid. Too restricting on UX.
I don't think you're not going to be able to get a silverlight control working in WPF.
Adding a footer row to the WPF datagrid is something a lot of people have complained about; it's ridiculous that it wasn't included out of the box.
See this thread from MSDN
Having been through this myself, your best bet will probably be to bite the bullet and use a third party control. It sucks, I know.

Data templates in WinForms

I'm trying to have a grid that has items that have a custom layout/feel. If you have a look at the following http://mdavey.files.wordpress.com/2007/05/ldmain.jpg this is kinda what I am trying to do (see the aqua/red pods on the left and the graphs on the bottom right).
Under WPF/ASP.Net I would know how to do this but in WinForms I have no idea (I have never had to work on a WinForms app before). Like in WPF I would use Data Templates.
Hence I was wondering anyone knows of any good resources that goes through this or if anyone knows if its just as easy as creating a custom control, they having a grid that has one column and setting the type of that column to the custom control and then binding the data source to the grid???
Cheers Anthony
You may check Essential Grid - it seems to me it supports features you need.
There is an example for progress bar cell which you could use for the graphs on the right side:
http://www.syncfusion.com/products/user-interface-edition/windows-forms/grid/progressbar-cell
And they have Push Button cell type which you could use for your pods on the left side:
http://www.syncfusion.com/products/user-interface-edition/windows-forms/grid/push-button
You might also check DevExpress XtrGrid:
http://www.devexpress.com/products/net/controls/winforms/grid/dataediting.xml
http://www.devexpress.com/products/net/controls/winforms/grid/appearance.xml
In any case, I would search for a component - it takes quite a lot of time to do such things from the scratch.

Setting the datasource of a WPF Grid

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

Categories

Resources