使用ItemControl时如何在C#中获取事件源
这是一个Wpf应用程序,我已经创建了6个图像。单击每个图像时,我希望显示一个页面。 Xaml代码与此类似使用ItemControl时如何在C#中获取事件源,c#,wpf,visual-studio-2010,xaml,C#,Wpf,Visual Studio 2010,Xaml,这是一个Wpf应用程序,我已经创建了6个图像。单击每个图像时,我希望显示一个页面。 Xaml代码与此类似 <Controls:ReflectionControl Grid.Row="2"> <ItemsControl ItemsSource="{Binding Path=DashBoardApps}" VerticalAlignment="Bottom" HorizontalAlignment="Center">
<Controls:ReflectionControl Grid.Row="2">
<ItemsControl ItemsSource="{Binding Path=DashBoardApps}" VerticalAlignment="Bottom" HorizontalAlignment="Center">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Controls:FishEyeControl />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock x:Name="txtAppName" Text="{Binding Path=ApplicationName}" TextAlignment="Center" Visibility="Hidden" FontSize="7px" Foreground="#eff7ff" />
<Image Source="{Binding Path=ApplicationImage}" Height="32" Width="32" MouseLeftButtonDown="Image_MouseLeftButtonDown_1"/>
</StackPanel>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="txtAppName" Property="Visibility" Value="Visible" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Controls:ReflectionControl>
到
cs代码:
private void Image_MouseLeftButtonDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
UserControl2 uc2 = new UserControl2();
pageTransitionControl.ShowPage(uc2);
canvas1.Visibility = System.Windows.Visibility.Hidden;
//canvas2.Visibility = System.Windows.Visibility.Visible;
canvas3.Visibility = System.Windows.Visibility.Hidden;
}
我想确定每个事件的源(从中生成事件的图像),并分配一个类似于上述代码的代码。如何执行此操作?在事件处理程序中,发送方是事件起源的对象
private void Image_MouseLeftButtonDown_1(object sender, ...
在您的情况下,您必须将其强制转换为
图像
,然后才有您的源。在事件处理程序中,发送方是事件的源对象
private void Image_MouseLeftButtonDown_1(object sender, ...
在您的情况下,您必须将其强制转换为
图像
,然后才有您的来源。回调(发送方
)的第一个参数是对正在单击的图像的引用。您必须尝试
来强制转换它。回调(sender
)的第一个参数是对正在单击的图像的引用。您必须尝试
才能使用它
总共有6张图片。因为我使用的是ItemsControl,所以我不知道是哪个图像触发了事件 在图像上使用带有Inputbinding的combinatino命令,并将其传递给CommandParameter您的datacontext
<Image>
<Image.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding MyCommand}" CommandParameter="{Binding}"/>
</Image.InputBindings>
</Image>
作为CommandParameter,您将在itemscontrol中拥有您的项
总共有6张图片。因为我使用的是ItemsControl,所以我不知道是哪个图像触发了事件 在图像上使用带有Inputbinding的combinatino命令,并将其传递给CommandParameter您的datacontext
<Image>
<Image.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding MyCommand}" CommandParameter="{Binding}"/>
</Image.InputBindings>
</Image>
作为CommandParameter,您将在itemscontrol中拥有您的项目是的,我尝试过这样做。我也找到了它的来源。我需要一个值来比较源。但我想不出什么。例如:var s=发送方作为映像。现在我想知道是哪个图像。因为我在这里使用的是itemscnrol,所以我无法准确地找到哪个映像。您可以添加一个绑定到数据集合的字典,其中映像源作为键,另一个值(应用程序名称?)作为值。然后查找与从发送方提取的源相对应的值。是的,我尝试过这样做。我也找到了它的来源。我需要一个值来比较源。但我想不出什么。例如:var s=发送方作为映像。现在我想知道是哪个图像。因为我在这里使用的是itemscnrol,所以我无法准确地找到哪个映像。您可以添加一个绑定到数据集合的字典,其中映像源作为键,另一个值(应用程序名称?)作为值。然后查找与从sender.var s=sender提取的源对应的值作为图像;s、 source=“我需要比较的东西”是的。一旦您将其转换为
图像
,您就可以实现任何逻辑了。var s=发送方作为图像;s、 source=“我需要比较的东西”是的。一旦您将其转换为图像
,您就可以实现任何逻辑了。总共有6个图像。因为我使用的是ItemsControl,所以我不知道是哪个映像触发了事件。总共有6个映像。因为我使用的是ItemsControl,所以我不知道是哪个图像触发了事件。