I have a menu in a WPF application that I would like to update each time the user open it because the MenuItems are from a DataBase that can change at any time.
Is there a way to rebuild theses MenuItems each time by subscribing to an event or something?
Regards
Consider binding the menu items to some sort of collection in your view model. The collection would contain the menu items, and the UI would bind to it, through a data template.
The blog post at http://blogs.msdn.com/b/llobo/archive/2007/10/23/binding-menus-using-heirarchicaldatatemplates.aspx should help describe this technique. Another post at http://zamjad.wordpress.com/2009/12/18/using-hierarchical-data-template-with-menu-item/ also contains good information.
Related
I have a xaml which contains a tab control (Name="MyTabControl"). I'm a beginner with wpf and in code and I want to dynamically add tab items that I then add a list box to each created tab item. Below is the code I have so far.
ListBox listbox = new ListBox()
TabItem tab = new TabItem()
tab.AddChild(listbox)
MyTabControl.Add(tab)
My issue is that I can't figure out how dynamically create new tabs that also would add a list box to each new tab and the new tabs then added to MyTabControl.
Then, I would want to be able to access each list box control, individually, in each tab to edit the list box content.
How is this done in code? How can i access the created list box controls to edit them?
WPF/UWP and XAML are designed with the MVVM pattern in mind. While you can use other approaches, doing so will miss about 90% of it's power and run into issues at every other corner.
In MVVM this would be simply a mater of Exposing a Collection and having a Tempalte targetting that type. ListBoxes might even have a custom Template system, but using ListBoxes might no longer be nessesary - any container can expose a Collection.
If you plan on learning MVVM, mid to longertem you should learn MVVM. I wrote a short intro a few years back, that should help you going. Help for people not following MVVM is parse on the Forum.
In general, it's a violation of the MVVM principles WPF is built around to build a UI in this way. Instead, consider a solution similar to the one proposed in the answers to this question. They do a good job of explaining both what to do, and why we do it this way.
Ok, this is going to be a 1000ft long question, but there's a lot to cover so here goes:
I am creating a paged items control, the purpose of which is to display very large collections in a paged format. I've created a repository on GitHub which can be found here. I have removed any styling for simplicity.
Upon starting the application, it looks like this:
This is pretty straightforward really, there's navigation buttons, an items per page selector but that isn't really important. The problem here is when you click the button "Open New Window".
This will open a new MainWindow, but on the first window, the collection disappears, as shown below:
The image above shows the old window in front, as you can see, there is no list of content as there is on the new window.
So, after smashing my head against a wall for a couple of hours, I am in need of assistance. I'll provide an overview of how the project is structured.
AnagramPagedItemsControl
The control being used for displaying the content is a custom control called AnagramPagedItemsControl, it is responsible for handling navigation between pages. I think the key property here is the PagedCollection.
The PagedCollection dependency property holds the collection which is bound to the Models property in the TestItemsViewModel.
TestItemsViewModel
This is the DataContext of the MainWindow, each window instance should create it's own view model. The CreateTestItems() method is responsible for creating the list of test items.
LazyPagedCollection
The purpose of this collection is to encapsulate the logic of a paged observable collection, it only loads pages when they are needed, hence the laziness.
It exposes methods like NextPage which are called in the AnagramPagedItemsControl when the user clicks on the various navigation buttons. The view model can also call navigation on the LazyPagedCollection, this allows the view model to call navigation without having to go through the view to do it.
TL;DR
When I create a new Window, the content of the previous window disappears. The problem is almost certainly with the control however I am stuck as to how to fix the problem.
This is quite a large problem to look at so I'd be very grateful for anyone who can look into it. Again, the source code is here, please feel free to suggest alternatives or pick out anything that I may have overlooked.
Had some time to spare, so:
The problem is the setter for the CollectionView property in the style for AnagramPagedItemsControl in generic.xaml.
This does not instantiate a new ListBox every time the style is applied; it will just create the one ListBox, the first time the style is created, and use that value over, and over again. So in effect, every instance of MainWindow shares the same ListBox.
You can see this by setting the Tag property of PART_CollectionView to (for instance) "1" in SetupBindings(ItemsControl PART_CollectionView). When you open a new window, you'll see that PART_CollectionView.Tag contains the same value you previously assigned.
I am wanting to build a WPF custom control that displays validation errors - one at a time - in a ribbon that goes across the top of the screen.
An ItemsControl or Selector sounds like a potential base class candidate, but my requirement is that only one item is shown at a time and the user will click on forward and back buttons on the ribbon to navigate through the validation errors.
Is it possible to use an ItemsControl in this way? To only show one item at a time?
Is there a better strategy for this?
I appreciate your thoughts and expertise!
This article should cover everything you want to do. The FlipView basically behaves like an ItemsControl and shows one item at a time by providing forward/back buttons.
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
The title of the question is bad, feel free to update it.
I have an observablecollection that generates a user control whenever it an item has been added to it.
However there are times where I just want to add to the collection and not have it create a usercontrol. Is there a way around this problem? Thanks.
Create a collection view around the observable collection and bind the UI to that. The collection view will have a filter so that only items you want in the UI are filtered in