简体   繁体   English

CallMethodAction在MainWindow中起作用,但在UserControl中不起作用

[英]CallMethodAction works in MainWindow, but not in UserControl

  1. It works, if I place the code in MainWindow. 如果我将代码放在MainWindow中,它将起作用。

  2. It doesn't work, if I place the code in UserControl and then place UserControl in MainWindow. 如果我将代码放在UserControl中,然后再将UserControl放在MainWindow中,则它不起作用。

I suppose the problem is with binding. 我想问题在于绑定。 How to make it work? 如何使其运作?

  1. The next works: 下一个作品:

MainWindow.xaml : MainWindow.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>
  1. The next doesn't work: 下一个无效:

MainWindow.xaml : MainWindow.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">
<Grid>
    <local:UserControl1></local:UserControl1>
</Grid></Window>

UserControl1.xaml : 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">
<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>

UserControl2.xaml.cs : UserControl2.xaml.cs

public partial class UserControl2 : UserControl
{
    public UserControl2()
    {
        InitializeComponent();
    }

    public void Ok()
    {
        MessageBox.Show("a");
    }
}

The solution is to move i:Interaction.Triggers below Grid. 解决方案是将i:Interaction.Triggers移到Grid下面。

Modify UserControl1.xaml to next: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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