在 WPF/C 中的新 window 中传递图像#

Pass image in a new window in WPF/C#

我在列表框中有一些图像。当用户点击一张图片时,我想打开一个新的 window (ImageWindow) 并在新的 window 中显示点击的图片。我已经添加了一个新的 XAML 文件和一个事件处理程序。这是我得到的:

主窗口:

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

图像窗口:

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

如何传递点击的图片?

查看example(点击图片)

你可以这样开始:

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

添加对 Microsoft.Expression.InteractionsSystem.Windows.Interactivity 的引用以使其正常工作。

只需复制、粘贴并调整此代码,使其适合您的变量名:

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

图像窗口:

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
}