2013-09-12 3 views
2

У меня есть экран, который выглядит следующим образом:WPF Binding Кнопка Команда

Screen Snapshot

Каждая цветная область другой UserControl положить в MainView.xaml следующим образом:

<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}}" /> 

(Я использую MVVMLight, поэтому локатор - это Vi ewModelLocator)

Зеленая зона является областью ItemsControl отображения кнопок для каждого из элементов (Компания & 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> 

MainContentVM содержит ChangePageCommand следующим образом:

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

      } 
     } 

Мой вопрос есть, как мне привязать кнопки в ItemContainerTemplate к ChangePageCommand в MainContentVM? То, что у меня не работает, при отладке ChangePageCommand никогда не вызывается при нажатии любой кнопки.

Когда я использую Command="{Binding ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type local:NavigationView}}}", не следует установить привязку команды к значению NavigationView (DataContext = MainContentVM)?

ответ

3

попробовать это:

<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}}}" /> 
+0

После изменения к тому, что вы предложили мои кнопки теперь светло-серый цвет, а текст является средний серый и нажав на кнопку все еще не вызывает ChangePageCommand. – BrianKE

+0

@BrianKE, далее читайте свой 'ChangePageCommand', не должен ли вы предоставить параметр (' IPageViewModel') с привязкой к команде? кнопка неактивна, потому что 'p => p является IPageViewModel' возвращает' False', поэтому кнопка отключена. – Bolu

+0

Это исправлено. Должно быть, он случайно удалил его при попытке различных решений привязки. – BrianKE