First of all, I use MVVM pattern in WPF.
I have a ViewModel which contains a grid. In order to render the grid in the view, I have to do the property of the grid public.
In this way the encapsulation of the control has been broken because if I want to use the control in other view models the grid is available for modifications.
Is there some solution about this problem?
I think Microsoft has screwed up, what is your opinion?
a viewmodel should not have a grid :) just the view. the viewmodel just have a collection for your grids itemssource.
edit: maybe what you want is some kind of usercontrol with Dependency Properties?
Related
I have an mvvm application...I need to have an editable listview.
I am binding my observable collection to listview.
How could I track changes of the values in listview ?...I mean if user edit an item...I need to have changed values in my observable collection.
If I use datagrid in WPFToolKit, is it easy ?
In a word, yes.
Take a look at data templates in WPF. They allow you to define how you want each item in your list (or any control) to appear and behave. So each item in your listview can one or more editable controls that are bound to each item in your collection (in this case an ObservableCollection). As you change data in the listview, the bound objects will in your collection will be updated in real time.
This is also possible with a datagrid.
Have a look at this link
http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-3-in-place-edit
It is recommended you to use the Datagrid.It already provides the edit mode functionality. You can use a TemplateColumn to provide editing views.
if you have an editable Collection in your viewmodel, just take a datagrid(editable stuff built in). you can create styles or use templates so that the datagrid look the way you want.
If I use datagrid in WPFToolKit, is it easy ?
yes ;) but if you can, use the .net4 datagrid
I have some questions about treeview in WPF.
How can I change header of selected item?
How can I add child Items to selected item?
Use MVVM. In your HierarchicalDataTemplate, bind Header and ItemsSource to properties in your view model which implement change notification. Now any changes you make to those properties will be reflected in the TreeView.
If you're not using MVVM and data binding to populate the TreeView, you're wasting a lot of effort.
Robert is right, you should use MVVM and data binding. The tutorial that IMHO explained it best is here
I am trying to hook up an ICommand on the model to a button within the ItemTemplate of a Pivot control.
To get a link to the parent model from within the ItemTemplate I usually use ElementName specifying the Name I provided for the xaml page.
This works when I use a ListBox to contain the items but not a pivot control.
Does any one have any ideas or come across this problem before?
Just noticed that if I define the PivotItems in xaml the Binding works. So it is only failing when I am dynamically populating the Pivot control.
UPDATE : OK So I'm beat with this now. I have linked the event to the models ICommand in the views code behind (nasty) and I'm going to look # this later. I will post my solution here once I have found it but any help would be great.
This is a know problem in Silverlight 3. Since, WP7 uses it right now, you will face the same thing with it as well.
To fix that, wrap your DataTemplate content that you put in your ItemTemplate into a UserControl.
Look into this question for further details.
WP7: Why does a ListBox.ItemsPanel break my ElementName data binding?
I can think of two ways to implement dependency properties that are shared between the detail views:
Store them in the master view model and add data bindings to the detail view models when they are created, and bind to them in the detail view.
Don't store them in the view models at all, and use FindAncestor to bind directly to properties of the master view instead.
What are the pros and cons of each, and are there other/better options?
Edit:
To clarify, I have a custom control (derived from Control) that uses a master view model for its DataContext. The custom control's control template contains an ItemsControl with ItemsSource bound to a dependency property in the master view model. This dependency property is an ObservableCollection of detail view model objects. The ItemsControl's item template binds to properties in the detail view model. What I need is a single property that is shared by the control template of the custom control (master view) and the item template for all the items in the ItemsControl. The custom control will contain a Slider or something to set the value, and the item template will simply read it.
Sorry if I'm abusing the terms, I'm still trying to get the hang of MVVM. If the problem still is unclear, I can try to write some code for a simple test case.
Implementing the dependency properties in the the View's code behind is the most popular practice I have seen. This allows you to interact with the DP via Databinding to the ViewModel. There aren't really any cons, DPs are meant to go one the object that needs to implement databinding.
If the above is not what you are asking, please include a brief Code Example to.
I've been doing a prototype in WPF without using MVVM. It's got to such a size now that I'm refactoring it to use MVVM.
When I started the project, I jumped straight in and created UserControls for lots of things.
I'm now breaking things in Views and ViewModels. But, I'm ending up with Views that contain UserControls; the UserControls have bindings to what are now objects in the Model.
So, is the notion of UserControls now dead in MVVM? What I mean is, in the past (WinForms, ASP.NET etc.) you'd have a project called 'Controls' and reuse these. Is the 'View' in MVVM a direct replacement of the typical UserControl?
A UserControl in WPF is little more than a ContentControl with a few tweaked default property values. A ContentControl is little more than a piece of content that can have a template applied to define its look.
The way I do MVVM skips the middleman and defines views as DataTemplates. Then you need only stick your VM into WPF's visual tree somewhere, and WPF will render it with your DataTemplate. For example:
<ContentControl Content="{Binding SomeViewModel}"/>
<ItemsControl ItemsSource="{Binding SomeViewModels}"/>
The way I see UserControls in the MVVM world is as a View. Rather than thinking of your WPF form as a single view, you can think of it as a composite of one or more views. So a UserControl can encapsulate a standard re-usable view that can be incorporated into multiple composite views. Great for re-usability and still they're still testable.
Mmmm... when I had user controls I just passed the DataContext from the View to the user control (the subinfo needed by that user control). But is true, that sometimes is hard to fit ViewModel with Views, UserControls, ...