views:

329

answers:

1

I'm getting an odd problem when implementing a master / child view and custom dependency properties.

Within my master view I'm binding the view model declaratively in the XAML as follows:

DataContext="{Binding MainViewModelProperty, Source={StaticResource Locator}}"

and my MainViewModel is exposing an observable collection which I'm binding to an ItemsControl as follows:

        <ItemsControl ItemsSource="{Binding Lists}" Height="490" Canvas.Top="10" Width="70">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Canvas>
                    <local:TaskListControl Canvas.Left="{Binding ListLeft}" 
                                           Canvas.Top="{Binding ListTop}" 
                                           Width="{Binding ListWidth}" 
                                           Height="{Binding ListHeight}"
                                           ListDetails="{Binding}"/>
                    </Canvas>    
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>    

TaskListControl in turn declares and bind to it's ViewModel and I've also defined a dependency property for the ListDetails property.

The ListDetails property is not being set and if I remove the declarative reference to it's viewmodel the dependency property's callback does get fired.

Is there a conflict with declaratively binding to viewmodels and definig dependency properties?

I really like MVVM Light's blendability and want to perserve with this problem so any help would be apprectiated.

If you'd like to receive the source for my project then please ask

A: 

Hi,

I am not sure I totally understand your problem, but let's try and guess.When you talk about "declaratively binding to viewmodel", do you actually mean "imperatively", as in "in the code" instead of "in the XAML"?

If this is the case, then you need to understand that this is overriding the DataContext inheritance from the parent, and the ListDetails property now refers to the TaskListControl DataContext, and not to the DataTemplate's DataContext anymore.

This is easy to change however, for example with:

<ItemsControl ItemsSource="{Binding Lists}" 
              Height="490" 
              Canvas.Top="10" 
              Width="70">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Canvas x:Name="RootPanel">
                <local:TaskListControl Canvas.Left="{Binding ListLeft}" 
                                       Canvas.Top="{Binding ListTop}" 
                                       Width="{Binding ListWidth}" 
                                       Height="{Binding ListHeight}"
                                       ListDetails="{Binding ElementName=RootPanel, 
                                           Path=DataContext}"/>
            </Canvas>    
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Would that work? Laurent

LBugnion