I am writing my application in Visual Basic using WPF. WPF is a very powerful technology that allows rapid user interface development; however, it does not have a native mechanism for label persistence. That is, if I label a part of an image, the label will not automatically reconfigure itself with the image if the user resizes the application. This means if the user resizes my application while it is displaying the above image, the skull label will no longer be on top of the skull in the image.
The solution to this problem turned out to be trivial, I am just surprised that Microsoft did not create a feature to handle this user scenario; a scenario that, I would think, is relatively common.
To solve this problem, I had to store several data points.
- The original height and width of the image.
- The 4 margin distances (distances between the left, right, top, and bottom of the label from the left, right, top, and bottom of the image, respectively).
- The width and height of the label itself.
Normally a control object in a user interface is only represented by two data points, its x and y coordinates; however, I had to represent the label with the 6 data points mentioned in 2. and 3. to maintain label persistence.
Now for the algorithm:
- On a resize event, I calculate the change of both the height and the width of the image.
Dim widthMultiplier As Double = AnatomyImage.ActualWidth / oldWidth
Dim heightMultiplier As Double = AnatomyImage.ActualHeight / oldHeight - I then use these multipliers to reevaluate the margins, height, and width of the label.
Dim x As New Thickness With {.Top = Label1.Margin.Top * heightMultiplier, _
.Bottom = Label1.Margin.Bottom * heightMultiplier, _
.Left = Label1.Margin.Left * widthMultiplier, _
.Right = Label1.Margin.Right * widthMultiplier _
}
Label1.Margin = x
Label1.Width = Label1.ActualWidth * widthMultiplier
Label1.Height = Label1.ActualHeight * heightMultiplier
Thus, the label is always in the same relative location on the image because its height, width, and margins still "own" the same relative percentage of the image.
...Seriously though, why was label persistence not a property in WPF?!
No comments:
Post a Comment