New to MVVM: how to workout the View [closed] - wpf

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I've just got 3 classes: Topic, Example and Exercise. Topic is the main class where both Example and Exercise is linked to. In my window (non-MVVM) I am populating a listbox with my topics, and upon selecting an item in the listbox where the topics are bound to, I am getting the ID of that topic and again populating 2 listboxes (for examples and exercises) related to that topic.
As I am new to MVVM and still trying to understand it, I am at lost on how to convert this to MVVM. Should I have 3 views (3 usercontrols?) for each 3 classes (models)? In my current setting they are all in 1 window.

This is entirely up to you.
How do you normally split state and behavior across classes? There is no 'extra' rule for that in MVVM.
Think of reuse and single responsibility. This is applicable for View, ViewModel and Model classes.
It is possible to compose a View, ViewModel and Model out of multiple classes.
One thing I learned is that although it is possible to reuse a ViewModel for multiple Views (in fact some introductions to MVVM state that as benefit) I found that in general Views tend to change after a couple of releases and you'll quickly need to create a separate ViewModel for each View. Again, this is not a requirement of MVVM; it is good old object orientation.

Related

how is useful to use IsAsync in the binding of a combobox? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
When I set the items source of a combobox, I have the option to se IsAsync as true.
When is it useful to use this parameter?
Thanks.
IsAsync is most useful for properties whose values take take a long time to retrieve or set via binding. It can make your application remain responsive while the bound value is being retrieved/set. For values that are retrieved immediately, it is not at all useful.
Does your checkbox-bound value take a long time to retrieve/set its values? If not, then IsAsync is a waste.
I will admit I've never used IsAsync with a two-way binding. Usually I save it for read-only properties

Conditional logic vs duplication in MVVM [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
We have a module with its Views, ViewModels and Models and a requirement came in to have an exact copy of the module, but part of the functionality has to be read-only.
The read-only mode should be turned on by a configuration parameter. The Model part is the same - it will use the same data sources.
I see two possible solutions to this problem:
Change the existing Views and ViewModels, by manipulating controls' interactivity based on the param.
Downsides of this approach is additional conditional logic. We're worried that if we do this, another requirement might come in, where we have to add more conditional logic. Upside - no duplication.
Another way is to split the existing view's into multiple components and assemble the regular module version and the read-only version using those components. We could disable interactivity on the View level in read-only versions and inherit from the ViewModel of the View which we're splitting from and override needed behaviour. The upside would be modularity. Downside - duplication, multiple places to maintain when new components are added in the future.
What's the preferred approach to solve problems like this?
This is a little simpler than you think.
The most elegant approach is to have a flag or value contained within the viewmodel which represents the editable state of the data (this could simply be a true/false flag, but I've also seen cases where it can be represented as an enum because there are multiple conditions or states).
Then within the view you can bind the control's IsEnabled or IsReadOnly properties to that value (using a converter if necessary to change the value to a boolean).
Obviously impossible to give a definitive answer without knowing all the specifics, but I generally take the view that if something is only required once, or maybe even twice, then I'll take the quickest and easiest route. In your case that would be to add conditional logic to the existing classes.
At the third change, I'll review and see if it needs to be split out into a separate entity. but more often than not the third (or even second) change never comes. Don't code for the future, YAGNI.

Wpf custom control for cinema, stadium or hall [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've got a task to create in wpf control as close as possible to this one
from the link (please click "Select Tickets" tab to see a control)
I'd like to ask for any advise where to start from?
Should I use canvas? If It's not a canvas, How to use zoom?
There are many answers for this question. At first sight, I would try to follow the next steps :
Create a control for the dots -> A circle (Path) which has a dependency property for its color.
Create a dynamic Grid with the number of columns and rows that you want and insert in every cell the control that you`ve created in the first step with the right fill color.
The zoom should be achieved by adjusting the number of columns and rows in your dynamic grid.If the number of columns it is reduced, the custom control will be stretched and will appear as a zoomed region.
Keep in mind that you have to use ObservableCollections and TwoWay Bindings in order to get the updates on the UI.
Hope it helps! Good luck
You can use a ViewBox to enable the control to size to the available space.
#Dragosh's answer is a good option. But there are often seating layouts that are not a grid. I would recommend creating a custom ItemsControl that binds to a collection of 'Seats' that have properties outlining their Section, Row, etc. and location. That way you can create a DataTemplate and ItemStyle that places the seat control at the correct location.

Refresh a label on WPF page [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How do I refresh a label on WPF page.
Basically the label displays how many agents there are, so when I put all the users details in and click Create User. When I click the button, I need the label to refresh on that page.
I have tried setting the content to null and then reloading the data.
int NumberOfAgents = _Users.Where(w => w.RoleID == _Role.FirstOrDefault(a => a.RoleType == "Agent").RoleID).ToList().Count();
lblCountAgent.Content=null
lblCountAgents.Content = NumberOfAgents.ToString();
But this does not seem to work.
If anyone could help me out on this that would be great.
Your code seems ok, NumberOfAgents probably isn't what you think it is and should be checked in the debugger.
On a side note, please don't code this way in WPF. You shouldn't be programmatically setting properties like "Content", and most of the time you shouldn't be programmatically setting properties at all.
Instead, the "Content" property of your label should be bound to a "NumberOfAgents" property in a ViewModel class that can then be set (raising PropertyChanged) when you create a user. This will propagate through the UI via the binding system. Here is an MSDN tutorial on how to set up MVVM with WPF.

DataGrid that also supports a tree view [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
Do you know of any datagrid-like control that also supports a treeview. Like this (but still fully WPF-stylable/templatable):
Update: UI-virtualization would be another important feature.
Telerik has a TreeListView control - http://www.telerik.com/products/wpf/treelistview.aspx. It's not free, but I think it'll do what you want.
It probably wouldn't be that hard to create your own from styling a regular DataGrid w/ Groups to look like that. The extra advantage is things like sorting and resizing columns are already taken care of.
Just style the Group Templates to contain the +/- button, folder, and group name, and change your regular DataGrid style to contain a white background with no grid lines. Even the Lines wouldn't be that hard to do if you wanted them
I always find this site useful for figuring out the different DataGrid components
You may look over this Hierarchical Data Library or this PropertyGrid control or this DataGrig control or this xamDataGrid control.
Or Look over this TreeGridControl you may set property of colomn IsReadOnly to False and edit it to WPF(its simple).
Or you can make your own control, for example whith help of this "TreeView+DataGrid in XAML" article, in which you should change TextBlock to TextBox, and make some other changes. Or whith help of this "WPF TreeListView Control" article and comments to it.
This control is somewhat similar to your request: http://www.obout.com/grid/aspnet_master_detail_self_reference.aspx
They also have this linked tree/grid example: http://www.obout.com/grid/tree_grid.aspx

Resources