2013-09-05 5 views
0

페이지 및 트리보기가 있습니다. MVVM을 사용하고 있습니다.mvvm의 다른 데이터 컨텍스트에 바인드 된 하위 트리 뷰 항목에서 상위 항목 컨텍스트의 바인딩 경로를 지정하는 방법

내 페이지가 내 데이터 viewmodel 데이터 컨텍스트를 사용 중입니다. 내 트리 뷰는 내 뷰 모델의 다른 공용 객체에 바인딩됩니다. 이제 트리 항목 내에서 페이지보기 모델에서 명령을 바인드하려고했습니다. xaml을 어떻게 참조 할 수 있습니까?

코드입니다.

<TreeView Style="{StaticResource MyNodeStyle}" 
     ItemsSource="{Binding {**Object in Page ViewModel**)}" 
     ItemContainerStyle="{StaticResource TreeViewItemStyle}" 
     ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
     DockPanel.Dock="Bottom" Height="440"> 
<TreeView.ItemTemplate> 
<HierarchicalDataTemplate ItemsSource="{Binding Connections}" 
         ItemContainerStyle="{StaticResource ResourceKey=TreeViewItemConnectionStyle}" > 
    <WrapPanel> 
     <CheckBox VerticalAlignment="Center" 
       Command="{Binding {**Command in Main Page View Model** }}"  
       IsChecked="{Binding Status, Mode=TwoWay}" 
       Focusable="False" 
       Style="{StaticResource ResourceKey=TreeView_CheckBox_Style}" > 
     </CheckBox> 
     <TextBlock Text="{Binding Name}" Style="{StaticResource ResourceKey=treeTextBoxStyle}" /> 
    </WrapPanel> 

어떤 도움이 크게 appriciated!

+0

사용하려는 'ICommand'의 이름을 사용하십시오 ... – MoonKnight

+0

나는 그것을 사용했습니다. 내가 wpf inspector를보고있을 때 Chek 상자를 클릭하면 BindingExpression 경로 오류로 표시됩니다. –

+0

명령의 코드를 표시하십시오. AttachedCommands에 대해 들어 보셨습니까? – MoonKnight

답변

1

것은 당신이 다음 명령, 조쉬 스미스의 RelayCommand 클래스를 사용하는 경우 SomeMethod

public void SomeMethod(object o) { ... } 

및 객체 oCheckBox ES 상태 (IsChecked)를 개최한다이다

private RelayCommand updateRootConnection; 
public RelayCommand UpdateRootConnection 
{ 
    get { 
     return updateRootConnection ?? (updateRootConnection = 
      new RelayCommand(o => SomeMethod(o)); 
    } 
} 

. 이제 사용할 바인딩은 CommandParameter="{Binding RelativeSource={RelativeSource Self}}" 객체 o를 통해 명령에 IsChecked 상태를 통과

<TreeView Style="{StaticResource MyNodeStyle}" 
      ItemsSource="{Binding {**Object in Page ViewModel**)}" 
      ItemContainerStyle="{StaticResource TreeViewItemStyle}" 
      ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
      DockPanel.Dock="Bottom" 
      Height="440"> 
    <TreeView.ItemTemplate> 
     <HierarchicalDataTemplate ItemsSource="{Binding Connections}" 
            ItemContainerStyle="{StaticResource ResourceKey=TreeViewItemConnectionStyle}" > 
      <WrapPanel> 
       <CheckBox VerticalAlignment="Center" 
          Command="{Binding UpdateRootConnection}" 
          CommandParameter="{Binding RelativeSource={RelativeSource Self}}" 
          IsChecked="{Binding Status, Mode=TwoWay}" 
          Focusable="False" 
          Style="{StaticResource ResourceKey=TreeView_CheckBox_Style}"> 

       </CheckBox> 
       <TextBlock Text="{Binding Name}" 
          Style="{StaticResource ResourceKey=treeTextBoxStyle}" /> 
      </WrapPanel> 
     </HierarchicalDataTemplate> 
    </TreeView.ItemTemplate> 
</TreeView> 

입니다.

이 정보가 도움이 되었기를 바랍니다.

+0

감사합니다. 그러나 실제 문제는 확인란을 클릭 할 때 ICommand와 연결되지 않는다는 것입니다. 나는 똑같이 따라 갔지만 여전히 그 명령에 연계되어있다. 어떤 도움이 필요합니까? –

+0

당신이 말하는 것은 의미가 없습니다. 바인딩 된 명령을 구성하는 방법과 XAML 바인딩을 사용하여 명령을 호출하는 방법을 모두 보여주었습니다. 뭐가 잘못 되었 니? 지금 무슨 일이 일어나고있는거야? 예제를 사용하면 작동하지 않는 것은 무엇입니까? – MoonKnight