I’m developing a RAM simulation in WPF. When a create the memory, I set the size (1024 k) and I can create partitions setting the size and position.
Here are the classes:
public class Memory
{
public ObservableCollection<Partition> Partitions { get; set; }
public ulong Size { get; set; }
public Memory(ulong size)
{
this.Partitions = new ObservableCollection<Partition>();
this.Size = size;
}
}
public class Partition
{
public int Id { get; set; }
public ulong Size { get; set; }
public ulong Position { get; set; }
}
public class MemoryService
{
public Memory GetModel()
{
var model = new Memory2(1024);
model.Partitions.Add(new Partition() { Id = 1, Size = 512, Position = 0 });
model.Partitions.Add(new Partition() { Id = 2, Size = 256, Position = 512 });
model.Partitions.Add(new Partition() { Id = 3, Size = 256, Position = 768 });
return model;
}
}
My problem is how can I show this model into an UI? I’m planning to create something like this:

When I add an new Partition, it should show at the left a Position, and the width of each rectangle of the partition must be proportional of the sizes (IE the memory size is 1024 and the partition 1 has 512, so this partition is the half of the memory size in the image).
At this moment, I haven’t created any UI, but I guess I will need to create a new control, where I draw in a canvas. I’m really not sure about this, can I take a little help about how can I start? I really have no idea creating new controls.
Thanks a lot!
WPF (specifically XAML) is made for this kind of stuff. An
ItemsControlwith a customizedItemTemplatecan do this so elegantly, there’s absolutely no reason to create a custom control and draw it in code. To show you how simple it is, I’ve mocked up a simple prototype using mostly XAML.First, some preliminary changes to your memory model. To make the height calculation simpler, I’ve added a
RelativeSizeproperty to thePartitionclass. This is exactly what it sounds like, adoublevalue that is the result ofPartition.Size / Memory.Size. That’s the only model change I made.Now, the good stuff:
I’ve created a simple window with a single
ItemsControl. The items it uses are the partitions, obviously. For each partition, I create aGridwith two columns. In the first column, I write some text for the position in memory (I usedStringFormatto append the “k”). In the second column, I have anotherGridthat is composed of aBorderand the text fromId.The only logic here is in the height binding. What I’m basically saying here is, “bind the height of this border to the height of the parent grid multiplied by the partition’s relative size.” I created some code-behind for the multiply converter, but it’s dead-simple (and not production quality):
Because of this binding, as the
Windowresizes, the border height will be kept in sync. In other words, thisItemsControlwill always fit the available space. Here’s what it ends up looking like:Obviously, you have a lot of polishing to do. But this should give you a good idea of what XAML is capable of.