Share listbox across controls/pivot items on WP7 - c#

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.)

Related

What's the difference between a stackpanel with datatemplates or a listview with listitems

I'm creating a WPF program that consumes rest api data. I want to implement lazyloading and infinite scroll on the data and programmatically create and add either custom data templates or listitems very similar to this design
I'm just confused as to which approach to take and what benefits/costs each provides
Easy choices:
Everyone uses MVVM so use MVVM.
Data Templating is a fundamental of wpf and building UI in code is not recommended - so use data templating.
You can dynamically add templates to resources by building xaml as strings. This is the MS recommended way to build any dynamic UI. Those strings can come from flat files, a database directly or a web service and you can build them by manipulating txt files or serialising controls.
A huge plus of this is you have the markup "right there". So when things go pear shaped you can paste into an experimental solution and see the errors light up in the xaml or see what the user is seeing.
If datatype associated templating doesn't suit for some reason then you could write a datatemplateselector and put your logic in there.
I'm not sure how you expect that to scroll exactly but I'd go with a listbox, some datatemplates associated with a type per view. Assuming the items can have different views - you just seem to have that "gilded" button or tag as an option.
Load your data into viewmodels with one per row.
.Add to an Observablecollection which is a public property in a viewmodel.
Bind that to the itemssource of a listbox.
They are then templated into UI.
A listbox has a scroller built in but you could re-template if you wanted to scroll using some other approach.
A StackPanel is a Panel that arranges child elements into a single line that can be oriented either horizontally or vertically.
A ListView is an ItemsControl that you can bind to an IEnumerable of objects and is used to present a collection of items.
What you should do is to create an ItemsControl with an ItemTemplate that corresponds to a scrollable item in the list. There is a basic example available here and you will find a lot more examples online.

Extracting ListBox to separate file

So, i have quite complex ListBox, with own style and composite item templates (in every item, there are image and text, both with click events, which rises corresponding commands in global viewmodel). What would be the easiest way to re-use that ListBox?
Should i extract style only, or move it out as UserControl, or should i make new component? Everything is static (commands, markup) exept content (should be able to use different items lists).
In the perfect case, is should be (pseudocode):
Page 1 xaml
<MyListBox Content={Binding Page1List}>
Page 1 vm
ObservableCollection<Items> Page1List;
and so on for other pages.
EDIT: i found this article - that makes sence, but i think there should be easier way for that.
http://www.codeproject.com/Articles/14340/Creating-a-look-less-custom-control-in-WPF
I believe the solution you are looking for is a look-less control.

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.

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

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