RelativeSource 从未调用过
RelativeSource never called
我想将代码后面的命令绑定到 UserControl.Resources
中的用户控制按钮 (ICommand
)
所以我用RelativeSource写了一段代码。但是没用。
我尝试使用转换器进行调试来解决这个问题。
但即使这样也没有调用。
这是我编写的代码。如果我错了请告诉我。
谢谢。
XAML
<UserControl x:Class="Views.Common.SubMenuPanel"
xmlns:controls="...."
....
mc:Ignorable="d">
<UserControl.Resources>
<controls:SubMenuButton x:Key="TerminateSystem"
ButtonName="Terminate System"
[[DOESN'T WORK]] --> Execute="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=TerminateSystemCommand
, Converter={StaticResource DebugDummyConverter}}"
ImageURI="...png"/>
<CollectionViewSource x:Key="LoginViewSubMenuItems">
<CollectionViewSource.Source>
<system:ArrayList>
<StaticResource ResourceKey="TerminateSystem"/>
</system:ArrayList>
</CollectionViewSource.Source>
</CollectionViewSource>
</UserControl.Resources>
<Grid>
<ListBox ItemsSource="{Binding Source={StaticResource LoginViewSubMenuItems}}"/>
</Grid>
Code Behind
public partial class SubMenuPanel : UserControl
{
public ICommand TerminateSystemCommand => new RelayCommand(() => Do Something);
public SubMenuPanel()
{
InitializeComponent();
this.DataContext = this;
}
}
您正在将 ICommand
绑定到 Execute
属性。您应该绑定的 属性 是 Command
,即
<controls:SubMenuButton x:Key="TerminateSystem"
ButtonName="Terminate System"
Command="{Binding RelativeSource={RelativeSource AncestorType=
{x:Type UserControl}}, Path=TerminateSystemCommand
, Converter={StaticResource DebugDummyConverter}}"
ImageURI="...png"/>
假设按钮中的其他属性没有引起其他问题,应该可以解决问题。
编辑:
我包含了我已验证有效的代码。因为我没有你对 SubMenuButton
的定义,所以我只是使用 Button
来代替:
XAML:
<UserControl.Resources>
<Button x:Key="TerminateSystem"
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=TerminateSystemCommand}">
Say Hello World!!!!
</Button>
<CollectionViewSource x:Key="LoginViewSubMenuItems">
<CollectionViewSource.Source>
<system:ArrayList>
<StaticResource ResourceKey="TerminateSystem"/>
</system:ArrayList>
</CollectionViewSource.Source>
</CollectionViewSource>
</UserControl.Resources>
<Grid>
<ListBox ItemsSource="{Binding Source={StaticResource LoginViewSubMenuItems}}"/>
</Grid>
后面的代码:
public ICommand TerminateSystemCommand =>
new RelayCommand(() => Console.WriteLine("Hello World!!!"));
我想将代码后面的命令绑定到 UserControl.Resources
ICommand
)
所以我用RelativeSource写了一段代码。但是没用。
我尝试使用转换器进行调试来解决这个问题。
但即使这样也没有调用。
这是我编写的代码。如果我错了请告诉我。
谢谢。
XAML
<UserControl x:Class="Views.Common.SubMenuPanel"
xmlns:controls="...."
....
mc:Ignorable="d">
<UserControl.Resources>
<controls:SubMenuButton x:Key="TerminateSystem"
ButtonName="Terminate System"
[[DOESN'T WORK]] --> Execute="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=TerminateSystemCommand
, Converter={StaticResource DebugDummyConverter}}"
ImageURI="...png"/>
<CollectionViewSource x:Key="LoginViewSubMenuItems">
<CollectionViewSource.Source>
<system:ArrayList>
<StaticResource ResourceKey="TerminateSystem"/>
</system:ArrayList>
</CollectionViewSource.Source>
</CollectionViewSource>
</UserControl.Resources>
<Grid>
<ListBox ItemsSource="{Binding Source={StaticResource LoginViewSubMenuItems}}"/>
</Grid>
Code Behind
public partial class SubMenuPanel : UserControl
{
public ICommand TerminateSystemCommand => new RelayCommand(() => Do Something);
public SubMenuPanel()
{
InitializeComponent();
this.DataContext = this;
}
}
您正在将 ICommand
绑定到 Execute
属性。您应该绑定的 属性 是 Command
,即
<controls:SubMenuButton x:Key="TerminateSystem"
ButtonName="Terminate System"
Command="{Binding RelativeSource={RelativeSource AncestorType=
{x:Type UserControl}}, Path=TerminateSystemCommand
, Converter={StaticResource DebugDummyConverter}}"
ImageURI="...png"/>
假设按钮中的其他属性没有引起其他问题,应该可以解决问题。
编辑:
我包含了我已验证有效的代码。因为我没有你对 SubMenuButton
的定义,所以我只是使用 Button
来代替:
XAML:
<UserControl.Resources>
<Button x:Key="TerminateSystem"
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=TerminateSystemCommand}">
Say Hello World!!!!
</Button>
<CollectionViewSource x:Key="LoginViewSubMenuItems">
<CollectionViewSource.Source>
<system:ArrayList>
<StaticResource ResourceKey="TerminateSystem"/>
</system:ArrayList>
</CollectionViewSource.Source>
</CollectionViewSource>
</UserControl.Resources>
<Grid>
<ListBox ItemsSource="{Binding Source={StaticResource LoginViewSubMenuItems}}"/>
</Grid>
后面的代码:
public ICommand TerminateSystemCommand =>
new RelayCommand(() => Console.WriteLine("Hello World!!!"));