C# CallMethodAction在MainWindow中工作,但在UserControl中不工作

C# CallMethodAction在MainWindow中工作,但在UserControl中不工作,c#,.net,wpf,xaml,data-binding,C#,.net,Wpf,Xaml,Data Binding,如果我把代码放在主窗口中,它就会工作 如果我将代码放在UserControl中,然后再放在UserControl中,它就不起作用了 在主窗口中 我想问题在于绑定。如何让它工作 下一步工作: 主窗口.xaml: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns

如果我把代码放在主窗口中,它就会工作

  • 如果我将代码放在UserControl中,然后再放在UserControl中,它就不起作用了 在主窗口中

  • 我想问题在于绑定。如何让它工作

  • 下一步工作:
  • 主窗口.xaml

    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        x:Name="window" x:Class="WpfApplication1.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Background="Black">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDown" SourceName="rectangle">
            <ei:CallMethodAction TargetObject="{Binding ElementName=userControl2}" MethodName="Ok"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid>
        <Rectangle x:Name="rectangle" Width="100" Height="100" Fill="White"></Rectangle>
        <local:UserControl2 x:Name="userControl2"></local:UserControl2>
    </Grid></Window>
    
    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        x:Name="window" x:Class="WpfApplication1.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Background="Black">
    <Grid>
        <local:UserControl1></local:UserControl1>
    </Grid></Window>
    
    <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfApplication1"
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
         xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
         x:Name="userControl" 
         x:Class="WpfApplication1.UserControl1"
         mc:Ignorable="d">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDown" SourceName="rectangle">
            <ei:CallMethodAction TargetObject="{Binding ElementName=userControl2}" MethodName="Ok"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid>
        <Rectangle x:Name="rectangle" Width="100" Height="100" Fill="White"></Rectangle>
        <local:UserControl2 x:Name="userControl2"></local:UserControl2>
    </Grid></UserControl>
    
    public partial class UserControl2 : UserControl
    {
        public UserControl2()
        {
            InitializeComponent();
        }
    
        public void Ok()
        {
            MessageBox.Show("a");
        }
    }
    

    解决方案是将i:Interaction.Triggers移到网格下面

    将UserControl1.xaml修改为下一步:

    <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfApplication1"
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
         xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
         x:Name="userControl" 
         x:Class="WpfApplication1.UserControl1"
         mc:Ignorable="d">
    <Grid>
        <Rectangle x:Name="rectangle" Width="100" Height="100" Fill="White"></Rectangle>
        <local:UserControl2 x:Name="userControl2"></local:UserControl2>
    </Grid>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDown" SourceName="rectangle">
            <ei:CallMethodAction TargetObject="{Binding ElementName=userControl2}" MethodName="Ok"/>
        </i:EventTrigger>
    </i:Interaction.Triggers></UserControl>