2010-11-03 111 views
0

我在Window.Resources一些XAML代碼:DataTemplate綁定問題?

 <ContextMenu x:Key="ParentContextMenu"> 
      <MenuItem Header="MenuItem..." Command="{Binding SomeCommand}"/> 
     </ContextMenu> 
     <DataTemplate x:Key="ChildDataTemplate" DataType="{x:Type System:String}"> 
      <TextBlock Text="{Binding}" VerticalAlignment="Bottom" /> 
     </DataTemplate> 
     <HierarchicalDataTemplate x:Key="ParentDataTemplate" DataType="{x:Type ViewModels:IParentViewModel}" ItemsSource="{Binding Path=Agents}" ItemTemplate="{StaticResource ChildDataTemplate}"> 
      <StackPanel Orientation="Horizontal" ContextMenu="{StaticResource ParentContextMenu}"> 
       <TextBlock Text="{Binding ServerName}" /> 
      </StackPanel> 
     </HierarchicalDataTemplate> 

然後

<TreeView ItemsSource="{Binding Items}" ItemTemplate="{StaticResource ParentDataTemplate}"/> 

爲什麼SomeCommand未綁定到上下文菜單項?

我確定DataContext包含ViewModel,因爲其他命令運行良好。請有任何想法嗎?

+1

不知道你可以bing DateTemplates:P – 2010-11-03 10:01:46

+0

當你運行應用程序時,你會在輸出窗口中看到任何錯誤信息嗎? SomeCommand可能會出現一些綁定錯誤? – ihatemash 2010-11-10 04:24:56

回答

0

此XAML的任何問題。 所以,我認爲你需要重視你的班級實施IParentViewModel。 1)在我看來,你的SomeCommand就像SomeCommand : ICommand。驗證訪問修飾符是公共(公共class SomeCommand : ICommand

2)驗證命令屬性的訪問修飾符是公共 (如

public SomeCommand SomeCommand 
    { 
     get { return _someCommand; } 
      set 
      { 
       _someCommand = value; 
       OnPropertyChanged("SomeCommand"); 
      } 
     } 

3)確認您已經創建命令實例(private SomeCommand _someCommand = new SomeCommand();

另外,如果您有依賴項屬性,請這樣做。 希望它有幫助。