简体   繁体   中英

How to get the Source of an event in C# when using ItemControl

This is a Wpf application and I've created 6 images. On the click of each image I want to display a page. Xaml code is similar to this.

 <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:

 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.

First parameter of the callback ( sender ) is a reference to the image being clicked. You have to try to cast it.

There as 6 images in total. Since I'm using ItemsControl I don't know which image exactly has triggered the Event.

Use a Command in combinatino with a Inputbinding on the image and pass to the commandparametr your datacontext.

                <Image>
                <Image.InputBindings>
                    <MouseBinding Gesture="LeftClick" Command="{Binding MyCommand}" CommandParameter="{Binding}"/>
                </Image.InputBindings>
            </Image>

as commandparamter you will have your item in the itemscontrol

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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