I'm writing an application in WPF, using the MVVm toolkit and have problems with hooking up the viewmodel and view.
The model is created with ado.net entity framework.
The viewmodel:
public class CustomerViewModel
{
private Models.Customer customer;
//constructor
private ObservableCollection<Models.Customer> _customer = new ObservableCollection<Models.Customer>();
public ObservableCollection<Models.Customer> AllCustomers
{
get { return _customer; }
}
private Models.Customer _selectedItem;
public Models.Customer SelectedItem
{
get { return _selectedItem; }
}
public void LoadCustomers()
{
List<Models.Customer> list = DataAccessLayer.getcustomers();
foreach (Models.Customer customer in list)
{
this._customer.Add(customer);
}
}
}
And the view (no code behind at the moment):
<UserControl x:Class="Customers.Customer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:vm ="clr-namespace:Customers.ViewModels"
d:DesignHeight="300" d:DesignWidth="300"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit" >
<Grid>
<toolkit:DataGrid ItemsSource="{Binding AllCustomers}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" AutoGenerateColumns="True">
</toolkit:DataGrid>
<toolkit:DataGrid ItemsSource="{Binding SelectedItem.Orders}">
</toolkit:DataGrid>
</Grid>
</UserControl>
And dataaccesslayer class:
class DataAccessLayer
{
public List<Customer> customers = new List<Customer>();
public static List<Customer> getcustomers()
{
entities db = new entities();
var customers = from c in db.Customer.Include("Orders")
select c;
return customers.ToList();
}
}
The problem is that no data is displayed simply because the data context is not set. I tried to do it in a code-behind but is did not work. I would prefer to do it in a xaml file anyway. Another problem is with the SelectedItem binding - the code is never used.
Since this is using the MVVM paradigm, I would instance your ViewModel in the constructor for the View. My View/ViewModels typically follow this sequence of events:
View is instanced
View constructor instances ViewModel
ViewModel initializes
ViewModel runs data getting procedures(separate thread)
ViewModel calls OnPropertyChanged("") to alert View that something has changed; check everything
My ViewModel is instanced from the XAML codebehind (sorry this is in VB.NET, have not gotten around to learning C# well enough to trust myself with it):
Public Sub New()
MyBase.New()
Me.DataContext = New EditShipmentViewModel(Me) 'pass the view in to set as a View variable
Me.InitializeComponent()
End Sub
Initially I hoped to have something like
<UserControl>
<UserControl.DataContext>
<Local:EditShipmentViewModel>
</UserControl.DataContext>
</UserControl>
But that did not work out like I wanted it to.
Hope that helps a little.
Is this what you're looking for?
I set my viewmodel datacontext the same way I observed Blend4 to. That is, if my viewmodel is called MainViewModel, I reference it in the view like:
<UserControl.Resources>
<local:MainViewModel x:Key="MainViewModelDataSource" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MainViewModelDataSource}}">
...view xaml stuff
</Grid>
also, if you're loading data from a database in the constructor of your viewmodel, don't forget to add a helper method around it like:
if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
{
myCollection = new ObservableCollection<Blah>(something);
}
so that visual studio/Blend4 doesn't crash trying to retrieve the data from the database connection in the Designer. I personally load data in the constructor quite often, just because I need it right away, and for it to be cached in memory from startup.
:)
Related
I'm creating a UserControl in WPF, that is able to work for any object of type IMyNode. Basically, it receives an ObservableCollection through a dependency property, register to it and do some stuff.
In one of my usecase, I use in a control that uses(and need), an ObservableCollection of SomeSpecificNode. SomeSpecificNode is an implementation of IMyNode.
Currently, I've a binding error:
System.Windows.Data Error: 1 : Cannot create default converter to perform 'one-way' conversions between types 'System.Collections.ObjectModel.ObservableCollection`1[SomeSpecificNode]' and 'System.Collections.ObjectModel.ObservableCollection`1[IMyNode]'.
I understand why it happens, it doesn't know how to convert automatically an ObservableCollection<SomeSpecificNode> to ObservableCollection<IMyNode>.
What would be the correct approach to do this?
Using a converter would break the NotifyPropertyChange. Using a ObservableCollection<IMyNode> in my parent ViewModel would not work for the other control in the same page.
Thank you!
Here some pseudo code:
public class SomeSpecificNode: IMyNode{
}
public interface IMyNode{
}
public class ParentViewModel {
public ObservableCollection<SomeSpecificNode> SelectedNodes {get;}=> new ObservableCollection<SomeSpecificNode>()
}
<UserControl x:Class="ParentView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ch.VibroMeter.Xms.Configurators.Controls.ActionBar"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.DataContext>
<ParentViewModel/>
</UserControl.DataContext>
<StackPanel Orientation="Horizontal" Name="RootContainer">
<SomeChildControl Nodes="{Binding SelectedNodes}" /><!-- This binding will fail !-->
</StackPanel
</UserControl>
public partial class ParentView : UserControl
{
public static readonly DependencyProperty NodesProperty = DependencyProperty.Register(
nameof(Nodes), typeof(ObservableCollection<IMyNode>), typeof(ParentView), new PropertyMetadata(default(ObservableCollection<IMyNode>), OnNodesChanged));
private static void OnNodesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//...
}
public ObservableCollection<IMyNode> Nodes
{
get { return (ObservableCollection<IMyNode>)GetValue(NodesProperty); }
set { SetValue(NodesProperty, value); }
}
}
You should change the type of the dependency property to a compatible type such as IEnumerable<IMyNode>.
You cannot set an ObservableCollection<IMyNode> property to anything else than an ObservableCollection<IMyNode> or null.
An ObservableCollection<SomeSpecificNode> is not an ObservableCollection<IMyNode> but it is an IEnumerable<IMyNode> assuming that SomeSpecificNode implements IMyNode.
So this compiles just fine;
IEnumerable<IMyNode> collection = new ObservableCollection<SomeSpecificNode>();
But this doesn't:
ObservableCollection<IMyNode> collection = new ObservableCollection<SomeSpecificNode>(); //Cannot implictly convert type...
The difference is that IEnumerable<T> is covariant. Please refer to the docs for more information.
All -
I am using Unity in my WPF application for DI (without prism). I have my MainWindow.xaml and MainWindowViewModel.cs. I have a usercontrol in my Mainwindow.xaml. The user control has its own uc1.xaml and uc1viewmodel.cs. The UC1 ViewModel is currently exposed as a property on MainWindowViewModel so I can set the datacontext on the usercontrol (as recommended by many ppl here).
The question I have is how/where can I set this property - will it be in app.xaml.cs or will it be in the constructor of mainwindowviewmodel. Code Snippets:
App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
//Step 1 - One Time - Creating an instance of the container
UnityContainer unity = new UnityContainer();
//Step 2 - Registering your MainWindowViewModel
unity.RegisterType<IViewModel, UserControl1ViewModel>();
//Step 3 - Creating an Instance
UserControl1ViewModel uc1_mwvm = unity.Resolve<UserControl1ViewModel>(); <-- doesnt help
MainWindowViewModel mwvm = unity.Resolve<MainWindowViewModel>();
MainWindow mw = unity.Resolve<MainWindow>();
mw.Show();
}
MainWindowViewModel.cs
public class MainWindowViewModel
{
public IViewModel IVM { get; set; }
public MainWindowViewModel()
{
//IVM = new UserControl1ViewModel(); <-- All I really want is an equivalent but letting Unity do the work.
}
}
MainWindow.xaml
<Window x:Class="_05_ViewFist_UC_Unity_Working.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc1="clr-namespace:_05_ViewFist_UC_Unity_Working"
xmlns:uc2="clr-namespace:_05_ViewFist_UC_Unity_Working"
Title="MainWindow" Height="350" Width="525">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding NNN}" />
<uc1:UC1 DataContext="{Binding UC1VM}" />
<uc2:UC2 DataContext="{Binding UC2VM}" />
</StackPanel>
</Window>
UC1
<UserControl x:Class="_05_ViewFist_UC_Unity_Working.UC1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" >
<StackPanel Orientation="Horizontal" Background="Red">
<TextBlock Text="UC1 " />
<TextBlock Text="{Binding FirstName}" />
</StackPanel>
</UserControl>
As you see from the code - Instance of UC1 is created in xaml (MainWindow.xaml) and hence when MainWindow instance is created in app.xaml.cs - it still doesnt create an instance of UserControl1ViewModel.
Question again is : Dont think its a good practice for me to call the Unity Resolve statement in the constructor of MainwindowViewModel. Is that correct??
Can somebody share a code snippet of how/where I can do this?
Thanks
I downloaded your solution from github and tried to solve your problem.
You did a great job just you forgot few details such as property attributes.
This is how your App.cs file shall look alike:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
//Step 1 - One Time - Creating an instance of the container
UnityContainer unity = new UnityContainer();
//Step 2 - Registeration
unity.RegisterType<IMainWindowViewModel, MainWindowViewModel>();
unity.RegisterType<IUC1ViewModel, UC1ViewModel>();
unity.RegisterType<IUC2ViewModel, UC2ViewModel>();
//// Instance of MainWindowViewModel will be created once you call Resolve MainWindow.
MainWindow mw = unity.Resolve<MainWindow>();
mw.Show();
}
Here is what I changed:
public class MainWindowViewModel : IMainWindowViewModel
{
#region Public Properties
[Dependency]
public IUC1ViewModel UC1VM { get; set; }
[Dependency]
public IUC2ViewModel UC2VM { get; set; }
public string NNN { get; set; }
#endregion
public MainWindowViewModel()
{
NNN = "This value coming from MainWindowViewModel";
}
}
[Dependency] is a property attibute that tells Unity where to inject values.
I could merge my code to your repo in github if you wish so.
Let me know if this helped you any futher. Feel free to mark this as the answer.
You can use the service locator pattern. I use it with Unity as a DI.
internal class ServiceLocator
{
[...]
public MainViewModel Main { get { return container.Resolve<MainViewModel>(); } }
}
You can intantiate your class the way you want (DI or not, the class initializes the DI or receive it as a parameter, you can store the DI in a private static property, you can initialize your class if DI is null or when the application starts etc...).
In your App.xaml
<Application.Resources>
<vm:ServiceLocator x:Key="Locator"/>
</Application.Resources>
And now, you can set your datacontext
DataContext="{Binding Main, Source={StaticResource Locator}}"
Edit:
I found another way of doing it (among other):
Take a look at this article. In the command, you can resolve your viewmodel as you like.
I have a simple binding question as I feel I'm missing something fundamental in my view of how binding works.
I assume that since I've set the DataContext of my MainWindow to a ViewModel in code-behind, that all of the binding in MainWindow.xaml would assume source of this DataContext unless otherwise specified. This does not seem to be the case when I'm using my UserControl (which itself has a ViewModel driving it)
My scenario is best described in code:
MainWindow.xaml.cs
private ViewModels.MainMenuViewModel vm;
public MainWindow()
{
InitializeComponent();
vm = new ViewModels.MainMenuViewModel();
this.DataContext = vm;
}
MainWindow.xaml (using the data-context set in code-behind)
x:Class="Mediafour.Machine.EditorWPF.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:Machine.EditorWPF.Views"
xmlns:local="clr-namespace:Machine.EditorWPF"
xmlns:localVM="clr-namespace:Machine.EditorWPF.ViewModels"
Title="MainWindow" Height="350" Width="650">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<uc:MachineTreeView x:Name="MachineTreeView" Grid.Column="0" MachineDocument="{Binding Path=CurrentDocument}" />
MainWindowViewModel.cs
public class MainWindowViewModel : ObservableObject
{
public MainWindowViewModel()
{
OpenMachine(#"D:\Projects\Agnes\EditorWPF\Test.machine");
}
private void OpenMachine(string filePath)
{
MachineDocument currentDocument = MachineDocument.OpenFile(filePath);
CurrentDocument = currentDocument;
}
private MachineDocument _currentDocument;
public MachineDocument CurrentDocument
{
get { return _currentDocument; }
set
{
if (_currentDocument != null)
{
_currentDocument.Dispose();
_currentDocument = null;
}
_currentDocument = value;
base.RaisePropertyChanged("CurrentDocument"); //this fires
}
}
Using this approach, the binding statement in MainWindow.xaml errors out. Looking at Snoop binding error, it states that the CurrentDocument property is not found in MachineViewModel
System.Windows.Data Error: 40 : BindingExpression path error: 'CurrentDocument' property not found on 'object' ''MachineViewModel' (HashCode=27598891)'. BindingExpression:Path=CurrentDocument; DataItem='MachineViewModel' (HashCode=27598891); target element is 'MachineTreeView' (Name='MachineTreeView'); target property is 'MachineDocument' (type 'MachineDocument')
Why is it looking at the MachineViewModel when the binding is done in MainWindow?
Other binding properties in MainWindow do work as expected, this is the only UserControl binding I have though.
Either it is a simple mistake
you're setting MainMenuViewModel instead of MainWindowViewModel as MainWindow.DataContext
or maybe
you set the DataContext for the UserControl in the wrong manner. Take a look at this Simple Pattern for Creating Re-useable UserControls to do it the right way.
I have one ViewModel and two Views. How can I navigate to View2 from ViewModel. I read somewhere that we need to use PRISM, for opening multiple Views from ViewModel in Silverlight. Is there any alternative for PRISM?
Ideally you do not want to use view logic in your viewmodel. Your viewmodel should not know anything about the view. It would be a better idea for your viewmodel to set a property letting the view know it's time to navigate.
Here's an example:
ViewModel:
using System.ComponentModel;
namespace ViewModels
{
public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged == null) return;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private bool _DoneDoingStuff;
public bool DoneDoingStuff
{
get
{
return _DoneDoingStuff;
}
set
{
if (_DoneDoingStuff != value)
{
_DoneDoingStuff = value;
NotifyPropertyChanged("DoneDoingStuff");
}
}
}
}
}
View:
<navigation:Page
x:Class="Views.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:vm="clr-namespace:ViewModels">
<navigation:Page.Resources>
<vm:MyViewModel
x:Key="MyViewModelInstance" />
</navigation:Page.Resources>
<Grid
x:Name="LayoutRoot"
DataContext="{Binding Source={StaticResource MyViewModelInstance}}">
<i:Interaction.Triggers>
<ei:DataTrigger
Binding="{Binding DoneDoingStuff}"
Value="True">
<ei:HyperlinkAction
NavigateUri="AnotherPage.xaml" />
</ei:DataTrigger>
</i:Interaction.Triggers>
</Grid>
</navigation:Page>
Use a DataTrigger with Binding property set to the DoneDoingStuff property from your viewmodel, and the Value property set to "True". The DataTrigger will trigger when DoneDoingStuff from your viewmodel is set to true.
Now you need a trigger action to navigate. Use HyperlinkAction with the NavigateUri property set to the page you're navigating to.
Be sure to have System.Windows.Interactivity, System.Windows.Controls.Navigation, and Microsoft.Expression.Interactions assemblies in your references.
At first, this might seem to be too much, but your view logic is now where it needs to be.
You don't need to use PRISM, but it might be best.
One way that I have done it (and it is sloppy) is to have a MainView page that has a Navigation frame in it that will load the first view on start up. The MainView has to be a Page and not a UserControl. You need to have a navigation frame with uri mappings in the xaml and have a frame declared as shared/static in the code behind of the MainView Page and then set the loaded event (in the xaml) of the frame like so:
Public Shared MainContentFrame As Frame
Private Sub MainContentFrameXaml_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs)
MainContentFrame = TryCast(sender, Frame)
End Sub
Then in the viewmodel you can just call:
MainView.MainContentFrame.Navigate(New Uri("/SecondView", UriKind.Relative))
This probably violates the MVVM pattern in some way and may not be a good way to do it, but it works. This is how I used to do it, now I use PRISM.
I have an application which contains a hierarchy of View/Viewmodels.
ViewModelBase contains two ViewModels
private AViewModel _aViewModel = new AViewModel();
private BViewModel _bViewModel = new AViewModel();
My XAML binds a DataControl to
private ViewModelBase _currentView {get; set;}
public ViewModelBase CurrentView
{
get
{
return _currentView;
}
set
{
_currentView = value;
RaisePropertyChanged("CurrentView");
}
}
And decides which view to display based on DataTemplates
<DataTemplate DataType="{x:Type vm:AViewModel}">
<vw:AView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:BViewModel}">
<vw:BView />
</DataTemplate>
All this works fine but I'm not sure how to persist data between View changes. Say for example that AViewModel contains a string called "Test" and has a two way binding in AView. By changing view using CurrentView = _bviewmodel then my data won't persist when I change back to _aviewmodel - What's the best way to make sure any data stays between view changes as opposed to creating a new blank viewmodel each time.
I have to get _currentView to _aViewModel and then back to _currentView
Should have posted my AView xaml - Inside it was
<UserControl.DataContext>
<vm:AViewModel></vm:AViewModel>
</UserControl.DataContext>
So it appears I was stupidly creating a new ViewModel inside the Xaml everytime I changed the view. Thanks to everyone for pointing me in the right direction. I deleted this from the Xaml and all is working fine now.
Data should be persistent. Make sure that you don't create new ViewModels every time you change the CurrentView.