开发者

Binding issue with DataTemplate?

I have some XAML code in Window.Resources:

        <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" /&g开发者_如何学运维t;
        </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>

and then

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

Why is SomeCommand not bound to the Context menu item?

I am sure that DataContext contains a ViewModel because other commands are working well. Any ideas please?


Any problem with this XAML. So, I think you need to ckeck your class that implements IParentViewModel. 1) To my mind, your SomeCommand like SomeCommand : ICommand. Verify that access modifier is public(public class SomeCommand : ICommand)

2) Verify that access modifier of command property is public (e.g.

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

3) Verify that you have created command instance (private SomeCommand _someCommand = new SomeCommand();)

Also,do that in case if you have dependency property instead. Hope it helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜