简体   繁体   中英

Dependecy property ICommand doesn't work in nested control

I have a UserControl with DependecyProperty:

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:

<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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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