开发者

Using ViewModel as WPF DataContext Disposing problems

开发者 https://www.devze.com 2023-04-08 01:10 出处:网络
Just starting out using WPF / MVVM and have a question regarding using a viewmodel as the datacontext for a wpf window.

Just starting out using WPF / MVVM and have a question regarding using a viewmodel as the datacontext for a wpf window.

I'm using a view first approach and my view looks like this;

<Window x:Class="TestContext.TestForm"
 开发者_JAVA技巧   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TestForm" Height="300" Width="300"
    DataContext="{StaticResource testViewModel}">
    <Grid>
        <TextBox Text="{Binding Path=Address}" Height="23" HorizontalAlignment="Left" Margin="34,44,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
   </Grid>
</Window>

and I create my viewmodel in code like this ;

public class ViewModelFactory
{
    public TestViewModel CreateTestViewModel()
    {
        return new TestViewModel();
    }
}

I'm using the following approach in the app.xaml to create an instance of the viewmodel whenever I instantiate the view ;

<ObjectDataProvider x:Key="testViewModel" ObjectInstance="{StaticResource viewModelFactory}" MethodName="CreateTestViewModel">

This all works fine.

The problem is that I only ever see the viewmodel being created once. So new instances of the view use the same instance of the viewmodel.

I'd like to start out with a new instance of the viewmodel.

If I create the view model manually in the forms constructer ;

public TestForm()
{
    InitializeComponent();
    this.DataContext = new TestViewModel();
}

then it works as expected.

I'm trying to get my head around what's happening here...

Thanks....


Honestly using ObjectDataProvider isnt a good idea to get view models as that instance is single per App.

But if you insist to recreate the viewmodels you will have to refresh the testViewModel.

The way you do that is testViewModel.Refresh();


In Code

  1. Create your view
  2. Create Single Property on View (called VM)
  3. Create your viewmodel
  4. Assign the new viewmodel to VM
  5. Set DataContext to ViewModel
  6. Setup your bindings in XAML to point to the correct properties on your view model

As a rule

  1. I never use ObjectDataProviders or instantiate anything in XAML (too buggy)
  2. Always use the MVVM pattern
  3. Be careful of static resources - they are only set once per app instance where dynamic resources can be updated


Yes, Agree with @AngelWPF You should use set DataContext as View.DataContext = ViewModel;


You could instantiate you view model in the resources section of your view. Once you create new view - you'll get new view model.

Moreover, you'll get static bindings which are a bit performant and convenient to use because you'll get intellsense.

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication1="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <WpfApplication1:MyViewModel x:Key="ViewModel"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource ViewModel}">
        <ItemsControl ItemsSource="{Binding Data}"/>
    </Grid>
</Window>
0

精彩评论

暂无评论...
验证码 换一张
取 消