WPF - 单击用户控件内的按钮不会调用命令,也不会单击控件本身
WPF - clicking on a button inside an user control doesn't call the command, and neither does clicking on the control itself
我在将任何命令绑定到我的用户控件时遇到严重问题。一切都编译,但命令永远不会被调用。我尝试了两种方法 - 首先,我尝试将命令绑定到控件内的按钮,当我无法做到时,我尝试将命令绑定到控件本身的输入命令以查看它是否有效。它没有。控件本身位于 ItemsControl 中,以防万一。
这是我所做的简化版本。在控件的xaml.cs文件中:
public static readonly DependencyProperty CloseCommandProperty = DependencyProperty.Register(
"CloseCommand",
typeof(ICommand),
typeof(Thumbnail),
new UIPropertyMetadata(null)
);
public ICommand CloseCommand
{
get { return (ICommand)GetValue(CloseCommandProperty); }
set { SetValue(CloseCommandProperty, value); }
}
在 UserControl 的 xaml 文件中,有问题的按钮(UserControl 的名称="Control",哈希是另一个依赖项 属性):
<Button Command="{Binding ElementName=Control, Path=CloseCommand}" CommandParameter="{Binding ElementName=Control, Path=Hash}">
<TextBlock Text="X"/></Button>
现在,视图的 xaml 文件的简化(不包括无关属性)数据模板部分(如果重要的话,它有一个数据上下文),我在其中使用此控件:
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:Thumbnail Hash="{Binding Hash}"
CloseCommand="{Binding ElementName=Control, Path=DataContext.RemoveImageCommand}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
为了完整起见,我将包含来自视图模型的命令。
private bool CanRemoveImageCommandExecute(string hash)
{
return true;
}
private void RemoveImageCommandExecute(string hash)
{
MessageBox.Show("ABC","ABC");
}
public ICommand RemoveImageCommand
{
get { return new RelayCommand<string>(RemoveImageCommandExecute, CanRemoveImageCommandExecute);}
}
RelayCommand class 来自 MicroMVVM,它只是从两个函数创建一个命令(并且在其他任何地方都有效)。
你能告诉我为什么点击按钮没有反应以及如何解决这个问题吗?
看来,虽然我在那上面浪费了几个小时,但我问得太快了。从字面上看,发布后几分钟,我意识到我在 ItemTemplate 中的绑定是错误的。
问题是我使用了 ElementName 而不是 RelativeSource:
CloseCommand="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=local:AddImage}
其中 local:AddImage 是将 DataContext 设置为视图模型的视图的名称..
我在将任何命令绑定到我的用户控件时遇到严重问题。一切都编译,但命令永远不会被调用。我尝试了两种方法 - 首先,我尝试将命令绑定到控件内的按钮,当我无法做到时,我尝试将命令绑定到控件本身的输入命令以查看它是否有效。它没有。控件本身位于 ItemsControl 中,以防万一。 这是我所做的简化版本。在控件的xaml.cs文件中:
public static readonly DependencyProperty CloseCommandProperty = DependencyProperty.Register(
"CloseCommand",
typeof(ICommand),
typeof(Thumbnail),
new UIPropertyMetadata(null)
);
public ICommand CloseCommand
{
get { return (ICommand)GetValue(CloseCommandProperty); }
set { SetValue(CloseCommandProperty, value); }
}
在 UserControl 的 xaml 文件中,有问题的按钮(UserControl 的名称="Control",哈希是另一个依赖项 属性):
<Button Command="{Binding ElementName=Control, Path=CloseCommand}" CommandParameter="{Binding ElementName=Control, Path=Hash}">
<TextBlock Text="X"/></Button>
现在,视图的 xaml 文件的简化(不包括无关属性)数据模板部分(如果重要的话,它有一个数据上下文),我在其中使用此控件:
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:Thumbnail Hash="{Binding Hash}"
CloseCommand="{Binding ElementName=Control, Path=DataContext.RemoveImageCommand}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
为了完整起见,我将包含来自视图模型的命令。
private bool CanRemoveImageCommandExecute(string hash)
{
return true;
}
private void RemoveImageCommandExecute(string hash)
{
MessageBox.Show("ABC","ABC");
}
public ICommand RemoveImageCommand
{
get { return new RelayCommand<string>(RemoveImageCommandExecute, CanRemoveImageCommandExecute);}
}
RelayCommand class 来自 MicroMVVM,它只是从两个函数创建一个命令(并且在其他任何地方都有效)。
你能告诉我为什么点击按钮没有反应以及如何解决这个问题吗?
看来,虽然我在那上面浪费了几个小时,但我问得太快了。从字面上看,发布后几分钟,我意识到我在 ItemTemplate 中的绑定是错误的。 问题是我使用了 ElementName 而不是 RelativeSource:
CloseCommand="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=local:AddImage}
其中 local:AddImage 是将 DataContext 设置为视图模型的视图的名称..