2016-09-01 2 views
0
  1. Es funktioniert, wenn ich den Code in MainWindow platzieren.CallMethodAction funktioniert in MainWindow, aber nicht in UserControl

  2. Es funktioniert nicht, wenn ich den Code in UserControl platzieren und dann UserControl in MainWindow platzieren.

Ich nehme an, das Problem ist mit der Bindung. Wie funktioniert es?

  1. Die nächsten Arbeiten:

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. Die nächste nicht funktioniert:

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"); 
    } 
} 

Antwort

0

Die Lösung wird i bewegen: Interaction.Triggers unten Gitter.

Ändern UserControl1.xaml zum nächsten:

<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> 
Verwandte Themen