开发者

MVVM Light - master / child views and dependency properties

开发者 https://www.devze.com 2022-12-27 21:44 出处:网络
I\'m getting an odd problem when implementing a master / child view and custom dependency properties.

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

Within my masterView 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 am 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


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

0

精彩评论

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