简体   繁体   English

Dependecy属性ICommand在嵌套控件中不起作用

[英]Dependecy property ICommand doesn't work in nested control

I have a UserControl with DependecyProperty: 我有一个带有DependecyProperty的UserControl:

public static readonly DependencyProperty OpenCommandProperty =
        DependencyProperty.Register(
            "OpenCommand",
            typeof(ICommand),
            typeof(BaseRouteFlatView),
            new UIPropertyMetadata(null));

 public ICommand OpenCommand
 {
    get { return (ICommand)GetValue(OpenCommandProperty); }
    set { SetValue(OpenCommandProperty, value); }
 }

In Xaml: 在Xaml中:

<UserControl x:Name="myUserControl">
    <StackPanel>
        <Button x:Name="first" Command="{Binding OpenCommand, ElementName=myUserControl}"/> <!--Command works-->
        <controls:DropDownButtonControl>
            <controls:DropDownButtonControl.DropDownContent>
                <Button x:Name="second" Command="{Binding OpenCommand, ElementName=myUserControl}"/>  <!--Command doesn't work-->
            </controls:DropDownButtonControl.DropDownContent>
        </controls:DropDownButtonControl>
    </StackPanel>
</abstractions:UserControlBase>

What source I have to specify for working command in second button? 我必须在第二个按钮中为工作命令指定什么来源?

Try the following: 请尝试以下操作:

public UserControl()
{
    InitializeComponent();
    NameScope.SetNameScope(second, NameScope.GetNameScope(this));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM