C# Dependecy属性ICommand在嵌套控件中不起作用
我有一个具有DependecProperty的UserControl:C# Dependecy属性ICommand在嵌套控件中不起作用,c#,wpf,command,C#,Wpf,Command,我有一个具有DependecProperty的UserControl: public static readonly DependencyProperty OpenCommandProperty = DependencyProperty.Register( "OpenCommand", typeof(ICommand), typeof(BaseRouteFlatView), new U
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); }
}
在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>
我必须为第二个按钮中的工作命令指定哪个源?请尝试以下操作:
public UserControl()
{
InitializeComponent();
NameScope.SetNameScope(second, NameScope.GetNameScope(this));
}
可能重复。我已经看过这个主题。但它对我不起作用如果我想使用相对源而不是依赖属性,我应该怎么做?就像在DropDownButtonControl中一样,这种构造不起作用