Binding View Model to View in data template - wpf

public class ToolBarView : ToolBar
{
public ToolBarView()
{
this.DataContext = new ToolBarViewModel();
}
}
public ToolBarViewModel: ViewModelBase
{
public ObservableCollection<ViewModelBase> Items {get;set;}
public ToolBarViewModel()
{
// populate button view models
Items.Add(new ButtonViewModel() {Content="Button1"});
Items.Add(new ButtonViewModel() {Content="Button2"});
}
}
public class ButtonView : Button
{
public ButtonView()
{
this.DataContext = new ButtonViewModel();
}
}
public class ButtonViewModel : ViewModelBase
{
public object Content {get;set;}
}
In MainWindow.xaml
<Window.Resources>
<DataTemplate x:Key="buttonTemplate" DataType="{x:Type vm:ButtonViewModel}">
<v:ButtonView Content={Binding Content}/>
</DataTemplate>
<v:ToolBarView ItemsSource="{Binding Items}"
ItemTemplate={StaticResource buttonTemplate}/>
Note: I did INotifyChanged in ViewModelBase class
In MainWindow.xaml. i think My template is wrong.ButtonView in DataTemplate is creating a new view instance. It is not binding the viewModel that was poplulated in the ToolBar Items collection. I tried to do with Relative Binding. Still not successful.
Please help me out.

Just drop the line where you create a new VM and overwrite the DataContext:
this.DataContext = new ButtonViewModel();
Then the DataContext will be inherited (it will be the item in the collection, the ButtonVM).
(As a side-note, you seem to try view-first and view-model-first at the same time, you should stick with one. Also the view should probably already bind to all the relevant properties on the view-model so that you just need need to create the view and that's it)

Related

How to bind a DataTemplate datatype to a View which receives ViewModels a dependency injected by DI

I have a WPF application which implements navigation using MVVM, filling a different DataTemplate for each View within the same Window e.g. :
<Window.Resources>
<DataTemplate DataType="{x:Type foo:FooViewModel}">
<foo:FooView/>
</DataTemplate>
<DataTemplate DataType="{x:Type bar:BarViewModel}">
<bar:BarView/>
</DataTemplate>
<Window.Resources>
(Switching between Views/UserControls using MVVM is Rachel Lim's article which has inspired the aforementioned approach)
What happens now is that FooView gets FooViewModel automatically injected as a dependency by DI (which in my case is Microsoft.Extensions.DependencyInjection on .Net Core3 Preview) e.g. :
public partial class FooView : UserControl
{
public FooView(FooViewModel fooViewModel)
{
this.InitializeComponent();
this.DataContext = fooViewModel;
}
}
At this point obviously the DataTemplate complains because the FooView does not define a parameter-less ctor (as per reference Type '{0}' is not usable as an object element)
Is there any way to bypass this issue and let FooView to use FooViewModel as DataContext?
DataTemplate
<DataTemplate DataType="{x:Type foo:FooViewModel}">
<foo:FooView/>
</DataTemplate>
will assign an instance of FooViewModel to FooView.DataContext.
this.DataContext = fooViewModel; line in FooView control is useless, because DataContext will be overwritten in such scenario. I would say, it is perfectly fine not to pass view model via contrustor. It can be accessed from DataContext:
public partial class FooView : UserControl
{
public FooView()
{
this.InitializeComponent();
}
private FooViewModel Vm { get { return this.DataContext as FooViewModel; } }
}
In the navigation pattern you are using, View is a receiver of ViewModel. Current ViewModel is set by AppViewModel:
public class AppViewModel
{
// simplified properties
public ViewModelBase CurrentViewModel {get; set;}
public ICommand ViewFooCommand {get;}
public ICommand ViewBarCommand {get;}
}
You are trying to make View an originator/producer of ViewModel, which conflicts with pattern.

WPF: ×™how to populate my ViewModel in XAML instead of code behind

So i have this ViewModel class:
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<Person> _persons;
public ObservableCollection<Person> Porsons
{
get { return _persons; }
set
{
_persons = value;
NotifyPropertyChanged();
}
}
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
And then create this ViewModel class and populate its Person list:
ViewModel viewModel;
ObservableCollection<Person> persons
public MainWindow()
{
InitializeComponent();
viewModel = new ViewModel();
viewModel.Porsons= persons;
}
And then my ListView:
<ListView ItemSource={Binding Persons}/>
So instead of binding this Persons list into my ViewModel class and then do this ItemSource can i do it in pure XAML or this is the right way ?
Instead of creating a ViewModel property on your view it is recommended to use it's DataContext (this link also shows how to set it using XAML). Also don't populate the view model in the view since most of the time the data resides in the model and the view should not know anything about any models (when following MVVM).
Please read the link above and visit the links you meet. Also read this article about MVVM. This gives you some basic knowledge to make it easier to understand how to use the WPF framework.
There are many variations of view model creation in XAML.
For example alternatively you can create it in the App.Xaml to make it globally accessible via the StaticResource markup extension and assign it to the individual controls's DataContext via a Style or use an ObjectDataProvider.
This example uses XAML Property Element declaration to create a ViewModel instance directly in the target view. This instance is locally accessible only.
ViewModel.cs:
namespace Example
{
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
this.Persons = new ObservableCollection<Person>();
}
private ObservableCollection<Person> _persons;
public ObservableCollection<Person> Persons
{
get => _persons;
set
{
_persons = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
View.xaml:
<Window x:Class="Example.MainWindow"
...
xmlns:local="clr-namespace:Example">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Grid>
<ListView ItemSource={Binding Persons}/>
</Grid>
</Window>
Yes, you can. But no, you most certainly do not want to.
To answer your question, let's say your Person class looks like this:
public class Person
{
public string Name { get; set; }
}
You can easily declare a list in XAML and bind it to a ListView (say) like this:
<ListView DisplayMemberPath="Name">
<ListView.ItemsSource>
<x:Array Type="{x:Type vm:Person}">
<vm:Person Name="Tom" />
<vm:Person Name="Dick" />
<vm:Person Name="Harry" />
</x:Array>
</ListView.ItemsSource>
</ListView>
The result of which is this:
Just because you can do this, though, doesn't mean you should. The whole point of MVVM is to separate your view layer from your view model layer. You should be able to run your entire application from a test build without creating a single view object at all. In asking this question what you are apparently trying to do is declare a data structure in your view layer, which is totally the wrong place to put it. Your view layer should be as "dumb" as possible, with only the weakest possible bindings to your view model layer where the actual logic is going on.

WPF Data binding to observableCollection

Can someone give me a little help with this one please
I'm trying to reorganise an app to MVVM and make better use of data binding, but am struggling with a little issue.
I have a viewmodel class
public class MainWindowViewModel
{
public ObservableCollection<DiagramElement> Elements { get; set; }
public MainWindowViewModel()
{
AppMachineList = new ListOfMachines();
Elements = new ObservableCollection<DiagramElement>();
}
}
in which I create an observablecollection of the DiagramElement class.
public class DiagramElement : Button
{
private Item linkedItem;
public Item LinkedItem
{
get { return this.linkedItem; }
set
{
this.linkedItem = value;
this.DataContext = this;
this.Template = (ControlTemplate)FindResource("ItemTemplate");
}
}
The DiagramElement class just extends the button class and adds its own controlTemplate.
Back in my MainWindow.xaml.cs class, I instantiate the viewmodel and from that, populate a stackpanel in MainWindow.xaml from the ObservableCollection.
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
MainWindowViewModel vm = new MainWindowViewModel();
vm.LoadMachines();
foreach(DiagramElement d in vm.Elements)
{
ItemList.Children.Add(d);
}
}
}
<StackPanel x:Name="ItemList" Orientation="Vertical"></StackPanel>
What I want to do is, do away with the foreach loop and the calls to ItemList.Children.Add(). And replace this with a binding to Elements in the viewmodel like below.
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
MainWindowViewModel vm = new MainWindowViewModel();
vm.LoadMachines();
this.DataContext = vm;
}
}
<StackPanel x:Name="ItemList" DataContext="{Binding Path=Elements}"</Stackpanel>
I can't get the elements to be added to the Stackpanel, the binding doesn't work. Any help gratefully received.
FYI, having a ViewModel with a collection of UI elements (in your case, buttons) violates the principles of MVVM -- the UI and model should not be co-mingled like this.
But the immediate problem is you cannot use a StackPanel -- it is a control container but does not support binding to lists of items. You need to use some kind of repeater like an ItemsControl.
<ItemsControl ItemsSource="{Binding Path=Elements}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- your DiagramElement should go here, something like
<DiagramElement LinkedItem={Binding Path=SomePropertyOnYourRevisedElement} />
-->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
But there's more work to be done. DiagramElement needs to have LinkedItem converted into a DependencyProperty (lots of examples of this online) and Elements needs to be a list of some sort of model object that just stores the properties needed for the DiagramElement (with no UI stuff).

How can I make a property of a ViewModel created in the constructor of one view accessible to another view created in a ContentPresenter?

I am writing a C# application using the MVVM (Model, View, View-Model) design pattern.
I have a UserControl called ucDesigner. This is the only UserControl inside the main window. The following code is the definition of the constructor of the ucDesigner from the file ucDesigner.xaml.cs:
public ucDesigner()
{
InitializeComponent();
DesignerViewModel designerVM = new DesignerViewModel();
DataContext = designerVM;
}
This code above creates a new DesignerViewModel and sets it as the DataContext of the ucDesigner view.
The following code is the definition of the DesignerViewModel:
public class DesignerViewModel : ViewModelBase
{
public DrawViewModel DrawVM { get; set; }
public ToolboxViewModel ToolboxVM { get; set; }
public DesignerViewModel()
{
DrawVM = new DrawViewModel();
ToolboxVM = new ToolboxViewModel();
}
}
The constructor of the DesignerViewModel creates a new DrawViewModel and assigns it as the value of the DrawVM property in the DesignerViewModel.
ucDesigner has a ContentPresenter with a DataTemplate which displays a ucDraw view (of type UserControl). The following code is from ucDesigner.xaml:
<ContentPresenter Grid.Row="1" Name="DrawingArea" Content="{Binding DrawVM}">
<ContentPresenter.Resources>
<DataTemplate DataType="{x:Type vm:DrawViewModel}">
<uc:ucDraw />
</DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
I want the instance of the DrawViewModel (called DrawVM) from the DesignerViewModel created in the ucDesigner constructor to be accessible to the ucDraw view that is created with the ContentPresenter.

WPF ObservableCollection in xaml

I have created an ObservableCollection in the code behind of a user control. It is created when the window loads:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Entities db = new Entities();
ObservableCollection<Image> _imageCollection =
new ObservableCollection<Image>();
IEnumerable<library> libraryQuery =
from c in db.ElectricalLibraries
select c;
foreach (ElectricalLibrary c in libraryQuery)
{
Image finalImage = new Image();
finalImage.Width = 80;
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(c.url);
logo.EndInit();
finalImage.Source = logo;
_imageCollection.Add(finalImage);
}
}
I need to get the ObservableCollection of images which are created based on the url saved in a database. But I need a ListView or other ItemsControl to bind to it in XAML file like this:
But I can't figure it out how to pass the ObservableCollection to the ItemsSource of that control. I tried to create a class and then create an instance of a class in xaml file but it did not work. Should I create a static resource somehow>
Any help will be greatly appreciated.
Firstly, the ObservableCollection is a local variable. What you need to do is have it as a private global variable and expose it with a public property. You can use the INotifyPropertyChanged interface to have the image data update automagically when the actual collection itself changes.
In your XAML, you then need to set the DataContext to self, and you can then directly bind your public property to the ItemsSource. You may want to use an ItemTemplate for displaying the items in a custom manner.
Cheers,
Adam
Example as requested:
In C#:
public MyWindowClass
{
public ObservableCollection<image> MyImageCollection
{
get;
set;
}
}
In XAML:
<UserControl
...
DataContext="{Binding RelativeSource={RelativeSource Self}}">
...
<ListBox ItemsSource="{Binding MyImageCollection}" ItemTemplate="*yourtemplateresource*" />
...
</UserControl>
Now, the reason that I mentioned using INotifyPropertyChanged is that if you try:
MyImageCollection = new ObservableCollection<image>();
The items in the listbox will not automatically update. With an ObservableCollection, however, you do not need to implement INotifyPropertyChanged for basic addition and removal of list items.
You have to set the DataContext of the UserControl to your collection:
DataContext = _imageCollection
You can do that in the UserControl_Loaded() method.
Next you need to bind the ItemsSource of the ListView in the XAML:
<ListView ItemsSource="{Binding}"/>
The {Binding} is equivalent to {Binding .} which binds to the DataContext of the UserControl. If you need "more stuff" in your DataContext you can instead create a class like this:
class ViewModel : INotifyPropertyChanged {
public ObservableCollection Images { get { ... } }
...
}
Use this class for the DataContext:
DataContext = new ViewModel();
And replace the binding to bind to the Images property:
<ListView ItemsSource="{Binding Images}"/>
Then you can add another property to ViewModel:
class ViewModel : INotifyPropertyChanged {
public ObservableCollection Images { get { ... } }
public String Message { get { ... } set { ... } }
...
}
And bind it to a control:
<TextBlock Text="{Binding Message}"/>
Remember to fire the PropertyChanged event when the Message property is changed in ViewModel. This will update the UI when view-model properties are changed by code.

Resources