【问题标题】:Pass image in a new window in WPF/C#在 WPF/C# 的新窗口中传递图像
【发布时间】:2015-02-05 09:27:28
【问题描述】:

我在 ListBox 中有一些图像。当用户单击一个图像时,我想打开一个新窗口(ImageWindow)并在新窗口中显示单击的图像。我已经添加了一个新的 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(点击图片)

【问题讨论】:

    标签: c# wpf listbox


    【解决方案1】:

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

    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
    }
    

    【讨论】:

      【解决方案2】:

      你可以这样开始:

      <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 的引用以使其正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-26
        • 1970-01-01
        • 1970-01-01
        • 2013-06-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-10
        • 1970-01-01
        相关资源
        最近更新 更多