How can I add an extra control to a XAML Grid? - c#

I have a Grid bound to an ObservableCollection. I want to have an extra item in the grid such that it displays as just another tile but is actually, e.g., a button. Microsoft's Finance app demonstrates the effect I want perfectly (screenshot below). The goal is to have a final tile that is not itself a member of the collection, but sits in the grid like any other item.
The top answer to a similar question mentions the CompositeCollection, but CompositeCollection does not seem to be available for Win8 apps.

You could always add and extra item at the end of the ObservableCollection, you're binding to.
You could handle this exception in the view model: add it during loading, properly handle adding and removing the rest of the items if you support that in your app. (That's the approach we took in one of our projects.)
Or you could derive your own class from ObservableCollection to handle all that and reuse it.
To have the extra item display differently from the others, you can use a DataTemplateSelector and select the right template based on the type name or some other property that differentiates the extra item from the rest.

Here's sample project which shows standard items template (GridView, with ListView for snapped view) that adds a "+" content item to the ItemsSource, which is used for the "Add New Item" action in the app.
"Add New Item" item in GridView / ListView

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.

class concept in WPF

I'm new in WPF and I need to group many components in one element and make and add new instance of that element in window for each student in database like 2 textblock plus 1 textbox for each student, how can i do something like that?
This is where WPF really shines - you can use an ItemTemplate or a DataTemplate to style the UI with the underlying data objects knowing absolutely nothing about how they are being presented.
Check out Data Templating for an introduction. Effectively an ItemTemplate is a template (definition) of how each item should be rendered. A DataTemplate goes a step further and gives you the ability to select which template to use based on the data item being bound to, so you can have a list containing different types of objects yet still show them all in the same list/repeater control on the screen.

Silverlight ListBox not updating when not in XAML tree

Simplified version of the problem. So concentrate on the technical issue not user friendliness. ;)
I have a listbox filled with items from an ObservableCollection.
It is showed in my first view.
By selecting an item in the list the user goes to a another view.
I swap this view in, so the first one is not in the xaml tree.
In the second view they can perform actions.
One of the actions makes the item go away from the top list (like passing the item on to another user).
The first view listens for that event and can remove the item from the list.
Items.Remove(item)
Problem is that the item doesn't always disappear from the ListBox (but it does from the underlying list, I've checked with debug). I think it might have something to do with the view not being displayed at the moment the collection is updated. I've tested with setting SelectedItem to null, thinking that selection might keep it there after removal, but it made no difference.
Is this a known limitation, having to be visible for the listbox update to take effect?
Can I trigger something to make the observablecollection re-fire the notification?

Updating a control from another control child

I have some problems trying to update components of surface elements. I dont know if my approach to the problem is wrong, since I'm new to the topic.
My point is represented by the following diagram
According to the option that is selected in the menu, load different user controls as only child of StackPanel but i'm habing problems for update the Listview from loaded user controls, example: when I save a new item I need to recharge the list of items in the ListView
MVVM would be a good pattern here. If you have a problem passing data between controls - why not introduce them on top of unified data layer? Consider this:
Three radio buttons in your Menu, each one's IsChecked property bound to Visibility property of your respective UserControl.
StackPanel holding all three UserControls
ListView bound to ViewModel's List<Item>
Each of your UserControls bound to ListView.SelectedItem: one of them using TextBlock for read-only, one using TextBox for editing. Third one would create new item in your List<Item>. You would have to create ItemTemplate for each or create one UserControl (since they look very much alike) and use DataTemplateSelector.
If you're not familiar with MVVM here is a good start. You can also use one of the existing frameworks like MVVM Light
You can create an event on your child
public delegate void HandleNAMEOFYOURHANDLEEVENT();
on your child class
public event HandleNAMEOFYOURHANDLEEVENT yourInstance;
to use it on your child class
if (!ReferenceEquals(yourInstance, null))
{
yourInstance();
}
and you declare it on your parent like other event.

Wrap items in a windows applications List View control

Is there a way to make long items wrap in a .net windows forms application, I have already got LabelWrap set to true, but it doesn't work, I am using Details View Type,
Thanks
ListView doesn't allow text-wrapping.
What I usually do is set "ShowItemToolTips" property true in such cases. Then user can see the whole text a ToolTip. This is the only simple option I can see with ListView to help in this case.
If you really want to support for text-wrapping you will have to either use a third party ListView or use a DataGridView.
EDIT
If you are using VS2010, Tile View is an option for you.
With the tile view feature of the ListView control, you can provide a
visual balance between graphical and textual information. The textual
information displayed for an item in tile view is the same as the
column information defined for details view. Tile view works in
combination with either the grouping or insertion mark features in the
ListView control.
The LabelWrap property only works for icons (View types of LargeIcon or SmallIcon), not Details. As others have already answered, you have to use DataGridView or a 3rd party listview such as ObjectListView.

Categories

Resources