c# - 如何复制整个网格并将其复制到 WPF 中的其他列表框
问题描述
我的窗口中有两个ListBox
es。我将左侧放置Listbox
在多个小图像上,这些图像在每个网格中动态填充ImageList
。Listbox
当我从左侧单击图像时,我在右侧放置了空白以显示一张展开的图像。
我已经列出了图像并在 中放置了点击事件Grid
,但我不知道下一步该做什么。
这就是我想要做的。
这是我的代码。
xml.cs
<ListBox Name="ImageList" ItemsSource="{Binding ImageList}" Grid.Column="0" Height="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="{Binding ElementName=ImageList, Path=ActualWidth}"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border>
<StackPanel Orientation="Horizontal">
<!--image and dimensions-->
<Grid Width="300" Height="360" Background="Transparent" MouseDown="Grid_Click">
<StackPanel>
<TextBlock ></TextBlock>
<Image Source="{Binding Path}"/>
<TextBlock >
<TextBlock.Text>
<MultiBinding >
<Binding Path=/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock >
<TextBlock.Text>
<MultiBinding StringFormat="">
<Binding Path=/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</Grid>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox Grid.Column="1"></ListBox>
代码背后
private void Grid_Click(object sender, MouseButtonEventArgs e)
{
// What should I do here?
}
解决方案
要以 WPF 方式执行此操作,您不必使用单击事件。
在 ListView 中尝试 SelectedItems。
Xaml 代码
<Window x:Class="sof3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:sof3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListView Grid.Column="0" ItemsSource="{Binding ImagePathList}" SelectedItem="{Binding SelectedImagePath, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding ImagePath}"/>
<Image Source="{Binding ImagePath}" Stretch="Fill" Width="200" Height="200"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Image Grid.Column="1" Source="{Binding SelectedImagePath.ImagePath}" Stretch="Fill" Width="200" Height="200"/>
</Grid>
</Window>
主视图模型代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace sof3
{
class MainViewModel : INotifyPropertyChanged
{
public ObservableCollection<Image> ImagePathList
{
get
{
if (_imagePathList == null) _imagePathList = new ObservableCollection<Image>();
return _imagePathList;
}
set
{
_imagePathList = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ImagePathList)));
}
}
private ObservableCollection<Image> _imagePathList;
public Image SelectedImagePath
{
get
{
return _selectImagePath;
}
set
{
_selectImagePath = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedImagePath)));
}
}
public Image _selectImagePath;
public event PropertyChangedEventHandler PropertyChanged;
}
public class Image : INotifyPropertyChanged
{
public string ImagePath {
get
{
return _imagePath;
}
set
{
_imagePath = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ImagePath)));
}
}
private string _imagePath;
public event PropertyChangedEventHandler PropertyChanged;
}
}
主要代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace sof3
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
private MainViewModel _instance;
public MainWindow()
{
InitializeComponent();
_instance = new MainViewModel();
this.DataContext = _instance;
_instance.ImagePathList.Add(new Image() { ImagePath = @"C:\Users\dskim\source\repos\sof3\sof3\bin\Debug\1.bmp" });
_instance.ImagePathList.Add(new Image() { ImagePath = @"C:\Users\dskim\source\repos\sof3\sof3\bin\Debug\2.bmp" });
_instance.ImagePathList.Add(new Image() { ImagePath = @"C:\Users\dskim\source\repos\sof3\sof3\bin\Debug\3.bmp" });
_instance.ImagePathList.Add(new Image() { ImagePath = @"C:\Users\dskim\source\repos\sof3\sof3\bin\Debug\4.bmp" });
}
}
}
//Change Image Path
推荐阅读
- android - 如何在没有 startService 的情况下使用 resultReceiver?
- c++11 - 从内部类继承并同时将其传递给外部模板?
- eclipse - 将 Codename One 4.4.0 作为 eclipse 插件安装时出错
- r - 将当前行与 r 中的后续行(按日期和组排序)进行比较
- android - 为什么我的 Pixel 2 上的 Wifi Mac 地址发生了变化(Android Pie)?
- java - 使用命令行环境导入 RiTa 的问题
- regex - Jmeter正则表达式提取器不重新调整任何值
- sql - SQL Left-Join - 如果两个值都不是 TableB 中的空值或 TableB 中缺少行,则获取值
- .net-core - 使用 bitbucket-pipelines.yml 将 gcloud 安装到 microsoft/dotnet:sdk 容器以自动化部署
- c# - 查找共享点文件的内容类型