简体   繁体   中英

Pass image in a new window in WPF/C#

I've got some images in a ListBox. When the user clicks one image, I'd like to open a new window (ImageWindow) and show the clicked image in the new window. I've added already a new XAML-file and a eventhandler. This is what I got:

MainWindow:

<ListBox Name="MainListBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <DockPanel HorizontalAlignment="Center">
                <Image Source="{Binding}" MouseDown="Image_MouseDown"></Image>
            </DockPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

/*========================================================================*/

private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
    ImageWindow imageWindow = new ImageWindow();
    //Pass image
    imageWindow.Show();
}

ImageWindow:

<ListBox Name="ImageListBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <DockPanel HorizontalAlignment="Center">
                <Image Source="{Binding}"></Image>
            </DockPanel>
        </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>

How do I pass the clicked image?

See example (click on the image)

Just copy, past and tune this code so it fits your varnames:

private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e) //Varname
    {

        ImageWindow imageWindow = new ImageWindow { Owner = this };
        foreach (var item in ListBox.Items) //Varname
        {
            imageWindow.ListBox.Items.Add(item);//Varname
        }

        imageWindow.SetSelectedImageIndex = ListBox.SelectedIndex; //Varname + save the index of the selected item and pass it to ImageWindow
        imageWindow.Show();
    }

ImageWindow:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Application.Current.MainWindow.WindowState = WindowState.Normal;
    ListBoxItem lbi = (ListBoxItem)ImageListBox.ItemContainerGenerator.ContainerFromIndex(SetSelectedImageIndex); //Get with the index the befor selected item
    lbi.Focus(); //Set the focus on it
}

You can start with something like this:

<Grid 
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions">

    <Popup x:Name="popup" PlacementTarget="{Binding ElementName=imageList}">
        <Image Source="{Binding PlacementTarget.SelectedItem , ElementName=popup}"/>
    </Popup>
    <ListView x:Name="imageList" >
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <ei:ChangePropertyAction PropertyName="IsOpen" 
                    TargetName="{Binding ElementName=popup}" Value="True"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListView>
</Grid>

Add references to Microsoft.Expression.Interactions and to System.Windows.Interactivity to get it work.

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