[英]How to get the Source of an event in C# when using ItemControl
This is a Wpf application and I've created 6 images. 这是一个Wpf应用程序,我已经创建了6张图像。 On the click of each image I want to display a page.
在单击每个图像时,我要显示一个页面。 Xaml code is similar to this.
Xaml代码与此类似。
<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>
I've associated the event 我已经关联了活动
MouseLeftButtonDown
to 至
Image_MouseLeftButtonDown_1
The cs code: 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;
}
I want to identify the source of each event(The image from which the event has been generated) and assign a code similar to the above code. 我想确定每个事件的来源(从中生成事件的图像)并分配与上述代码相似的代码。 How do I do it?
我该怎么做?
In events handlers, sender is the object from which the event originated 在事件处理程序中, 发送者是事件起源的对象
private void Image_MouseLeftButtonDown_1(object sender, ...
In your case, you'll have to cast it to Image
and then you'll have your origin. 就您而言,您必须将其投射到
Image
,然后您才能拥有原点。
First parameter of the callback ( sender
) is a reference to the image being clicked. 回调(
sender
)的第一个参数是对被单击图像的引用。 You have to try
to cast it. 您必须
try
投射它。
There as 6 images in total.
总共有6张图片。 Since I'm using ItemsControl I don't know which image exactly has triggered the Event.
由于我正在使用ItemsControl,因此我不知道哪个图像正是触发了该事件。
Use a Command in combinatino with a Inputbinding on the image and pass to the commandparametr your datacontext. 将combinatino中的Command与图像上的Inputbinding一起使用,并将您的数据上下文传递给commandparametr。
<Image>
<Image.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding MyCommand}" CommandParameter="{Binding}"/>
</Image.InputBindings>
</Image>
as commandparamter you will have your item in the itemscontrol 作为commandparamter,您将把您的项目放在itemscontrol中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.