如果我将代码放在MainWindow中,它就可以。
如果我将代码放在UserControl中,然后放置UserControl,它就不起作用 在MainWindow。
我认为问题在于绑定。如何使它工作?
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>
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 :
<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 :
public partial class UserControl2 : UserControl
{
public UserControl2()
{
InitializeComponent();
}
public void Ok()
{
MessageBox.Show("a");
}
}
答案 0 :(得分:0)
解决方案是在网格下移动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>