从 WinForms 中的`ImageList` 迁移到 WPF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15007467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Migrating from `ImageList` in WinForms to WPF
提问by AppFzx
I'd like to move a project to WPF and in it I have an ImageList(loaded by selecting 25 images in design view) that I use to create buttons with as follows:
I've simplified the project down to the following code. This is my whole project(aside from autogenerated code in .Designer.cs):
我想将一个项目移动到 WPF,其中我有一个ImageList(通过在设计视图中选择 25 个图像加载),用于创建按钮,如下所示: 我已将项目简化为以下代码。这是我的整个项目(除了 .Designer.cs 中的自动生成代码):
public partial class Form1 : Form
{
Button[] buttonList = new Button[25];
Size buttonSize = new Size(140, 140);
public Form1()
{
InitializeComponent();
this.ClientSize = new Size(142 * 5, 142 * 5);
for (int i = 0; i < buttonImages.Images.Count; i++)
{
buttonList[i] = new Button();
buttonList[i].Size = buttonSize;
buttonList[i].Location = new Point(
(i % 5) * (buttonSize.Width + 2) + 1,
(i / 5) * (buttonSize.Height + 2) + 1);
buttonList[i].Image = buttonImages.Images[i];
}
SuspendLayout();
Controls.AddRange(buttonList);
ResumeLayout(false);
}
}
I can't seem to wrap my head around how to do this trivial task in WPF. As best as I can tell from answers on here (like this)I should be
我似乎无法理解如何在 WPF 中完成这项微不足道的任务。尽我所能从这里的答案中看出(像这样)我应该
- Filling a folder with images
- Creating a ResourceDictionary referencing them
- Creating another file that references the ResourceDictionary reference
- Create a something else (I can't figure out what) that accesses the images through the ResourceDictionary to load them to buttons (but the button class doesn't even have a constructor...???!!!)
- 用图像填充文件夹
- 创建引用它们的 ResourceDictionary
- 创建另一个引用 ResourceDictionary 引用的文件
- 创建一个其他东西(我不知道是什么)通过 ResourceDictionary 访问图像以将它们加载到按钮(但按钮类甚至没有构造函数......???!!!)
Can someone help translate this to WPF? Honestly, I just can't figure out where to start.
有人可以帮助将其翻译成 WPF 吗?老实说,我只是不知道从哪里开始。
If it matters, here's what this looks like when it runs:

如果重要的话,下面是它运行时的样子:

采纳答案by ywm
We shall use MVVM for this.
为此,我们将使用 MVVM。
First we will make a model.
首先,我们将制作一个模型。
public class MyModel
{
public BitmapSource Picture { get; set; }
public string Description { get; set; }
}
Then our ViewModel
然后我们的 ViewModel
public class MyViewModel
{
public ObservableCollection<MyModel> Images { get; set; }
public ICommand ButtonClicked { get; set; }
... Logic to populate the images
}
And then our view
然后我们的观点
<Window x:Class="TestWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:TestWPF"
Title="MainWindow"
Height="350"
Width="525">
<Window.Resources>
<loc:MyViewModel x:Key="ViewModel" />
</Window.Resources>
<Grid DataContext="{StaticResource ViewModel}">
<ListView ItemsSource="{Binding Images}">
<ListView.ItemTemplate>
<DataTemplate>
<Button Command="{Binding ButtonClicked, RelativeSource=Parent}"
CommandParameter="{Binding Description}">
<Image Width="50"
Height="50"
Source="{Binding Picture}"></Image>
</Button>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>
That will create a list that should act in the same way.
这将创建一个应该以相同方式运行的列表。

