2

I have a screen that looks like this:

Screen Snapshot

Each colored area is a different UserControl put into the MainView.xaml as follows:

<UserControl Name="TopDockPanel" DockPanel.Dock="Top" Content="{Binding QuickInfoVM, Source={StaticResource Locator}}" />
<UserControl Name="LeftDockPanel" DockPanel.Dock="Left" Content="{Binding NavigationVM, Source={StaticResource Locator}}" />
<UserControl Name="RightDockPanel" DockPanel.Dock="Right" Content="{Binding MainContentVM, Source={StaticResource Locator}}" />

(I am using MVVMLight so Locator is the ViewModelLocator)

The green area is an ItemsControl area displaying buttons for each of the items (Company & Employee):

<UserControl x:Class="MvvmLightDemo.View.NavigationView"
             Name="Navigation"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MvvmLightDemo.View"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="120"
             DataContext="{Binding MainContentVM, Source={StaticResource Locator}}" >

    <StackPanel Background="CadetBlue">
        <ItemsControl ItemsSource="{Binding PageViewModels}">
            <ItemsControl.ItemTemplate>

                <ItemContainerTemplate>
                    <Button Content="{Binding Name}" 
                            Style="{StaticResource NavigationTextStyle}" 
                            Command="{Binding ChangePageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:NavigationView}}}" />


                </ItemContainerTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</UserControl>

The MainContentVM contains the ChangePageCommand as follows:

        public RelayCommand<IPageViewModel> ChangePageCommand
        {
            get
            {
                return _changePageCommand = new RelayCommand<IPageViewModel>(
                        p => ChangeViewModel((IPageViewModel)p),
                        p => p is IPageViewModel);

            }
        }

My question is, how do I bind the Buttons in the ItemContainerTemplate to the ChangePageCommand in MainContentVM? What I have doesn't work, when debugging the ChangePageCommand is never called when clicking either button.

When I use Command="{Binding ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type local:NavigationView}}}" shouldn't that set the binding of the Command to that of the NavigationView (DataContext = MainContentVM)?

1 Answer 1

3

try this:

 <Button Content="{Binding Name}" 
                            Style="{StaticResource NavigationTextStyle}" 
                            Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:NavigationView}}}" />

Update:

 <Button Content="{Binding Name}" 
                                Style="{StaticResource NavigationTextStyle}" 
                                CommandParameter="{Binding}"
                                Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:NavigationView}}}" />
Sign up to request clarification or add additional context in comments.

3 Comments

After changing to what you suggested my buttons are now light gray and the text is a medium gray and clicking on a button still does not call ChangePageCommand.
@BrianKE, Further read your ChangePageCommand, shouldn't you provide a parameter (a IPageViewModel) with your Command binding? the button grayed out because p => p is IPageViewModel returns False, so the button is disabled.
That fixed it. Must have accidentally deleted it when trying various solutions to the binding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.