Two viewmodels per usercontrol when binding to ItemsSource?

Topics: Questions
Nov 9, 2012 at 3:48 PM
As a test i created a simple ListView bound to an ObservableCollection<Hotel>

The constructor on the HotelViewmodel gets called twice per HotelUserControl.

I had the same issue when binding to the ItemsSource on a treeview 

Is this a bug or am i doing something wrong?

<catel:DataWindow 
        x:Class="TwoViewModelsPerUserControlTest.Views.MainWindow"
        xmlns:catel="http://catel.codeplex.com" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Views="clr-namespace:TwoViewModelsPerUserControlTest.Views" 
        xmlns:ViewModels="clr-namespace:TwoViewModelsPerUserControlTest.ViewModels"
        xmlns:Model="clr-namespace:TwoViewModelsPerUserControlTest.Model"
        Title="{Binding Title}" 
        Height="350" 
        Width="525"> 
    <catel:DataWindow.Resources>
        <DataTemplate DataType="{x:Type Model:Hotel}">
            <Views:HotelUserControl Content="{Binding}"/>   
        </DataTemplate>
    </catel:DataWindow.Resources>
        <ListView ItemsSource="{Binding Hotels}"></ListView>    
</catel:DataWindow >

    public class Hotel : DataObjectBase
    {
        public static readonly PropertyData DescriptionProperty = RegisterProperty("Description", typeof(string));      
        public string Description
        {
            get { return GetValue<string>(DescriptionProperty); }
            set
            {
                SetValue(DescriptionProperty, value);
            }
        }
    }

   public class HotelViewModel : ViewModelBase
    {
        public HotelViewModel(Hotel hotel)
        {
            Hotel = hotel;
        }      

        public static readonly PropertyData HotelProperty = RegisterProperty("Hotel", typeof(Hotel));
        [Model]
        public Hotel Hotel
        {
            get { return GetValue<Hotel>(HotelProperty); }
            set { SetValue(HotelProperty, value); }
        }

        public static readonly PropertyData DescriptionProperty = RegisterProperty("Description", typeof(string));
        [ViewModelToModel("Hotel")]
        public string Description
        {
            get { return GetValue<string>(DescriptionProperty); }
            set { SetValue(DescriptionProperty, value); }
        }
       
    }

 

Coordinator
Nov 10, 2012 at 2:02 AM

Ok, this can have a *lot* of reasons.

1) Did you use the catel base control or do you use the behavior on a control?

2) Is the complete control reloaded by external components or layout restoring?

3) Are you using the latest beta?

4) I don't understand this:

<Views:HotelUserControl Content="{Binding}"/>   

Shouldn't that be DataContext? And... you can skip it, the latest version picks this up automatically.

 

If you create a new issue and upload the demo, I can take a look at it.

Nov 14, 2012 at 6:52 PM

oh man, did i have a brainfart on this one.

I was setting the behavior

<i:Interaction.Behaviors>
        <catel:UserControlBehavior ViewModelType="viewmodels:HotelViewModel" />
</i:Interaction.Behaviors>

 and I was setting the GetViewModelType() in code behind

Sorry, about the confusion.

Coordinator
Nov 14, 2012 at 6:54 PM

ok, great to hear that you got it solved :)