Reputation: 2160
I have a viewbox with an image inside:
<Viewbox MaxHeight="100" MaxWidth="100" x:Name="Scenario4ImageContainer2" Stretch="Uniform" Grid.Column="1" Grid.Row="1">
<Image x:Name="Scenario4Image" PointerPressed="Scenario4Image_PointerPressed" HorizontalAlignment="Stretch" />
</Viewbox>
I want to be able to grab the actual width/height values, but when I try this in the backend C#:
int w = (int)Scenario4ImageContainer.Width
I get an error saying the parameter is incorrect.
This goes away if I hardcode the width, but I want it to resize dynamically.
I also tried grabbing Scenario4ImageContainer.ActualWidth but this parameter was "incorrect" as well.
Upvotes: 1
Views: 1097
Reputation: 2160
On every draw I do a quick check to see if the container size has changed (with an initialization of 0 at the beginning to make sure it catches the first time).
if (containerWidth != Output.ActualWidth - 300)
{
Scenario4ImageContainer.Width = Output.ActualWidth - 300;
Scenario4ImageContainer.Height = Output.ActualHeight - 20;
containerWidth = Output.ActualWidth - 300;
}
It works for the most part, but when the class gets navigated out and navigated back it has a problem for some reason. Probably unrelated.
Upvotes: 0
Reputation: 13698
Seems like I've found the event you need to handle: you should handle ImageOpened event. It is because image is retrieved asynchronously and if try to handle any other event there is a good chance to not have image loaded at that time so actual size is zero
Upvotes: 1
Reputation: 3046
A while back I was trying to measure width of a string. You can try a similar mechanism to get dimensions.
http://invokeit.wordpress.com/2012/09/19/how-to-measure-rendered-string-dimensions-in-win8dev/
this.tb.FontSize = 20;
this.tb.Measire(new Size(400, 300)); // assuming that 400x300 is max size of textblock you want
double currentWidth = this.tb.DesiredSize.Width;
double currentHeight = this.tb.DesiredSize.Height;
Upvotes: 1