2011-09-18 27 views
1

我有一个Image控件,在运行时生成。显示时呈现正确的大小。但我无法得到这个元素的大小。 WidthHeight是NaN。 ActualWidthActualHeight始终为0.0,并且控件永远不会触发事件SizeChanged事件。我无法获取UIElement的实际大小

enter image description here

我也产生在运行文本框,我不能让他们的规模太大。

生成图像的代码如下所示。

ExtendedImage image = new ExtendedImage(); 
image.Name = "element" + item.Id; 
if (item.Text.ToUpperInvariant().EndsWith(".GIF")) 
{ 
    var gif = BookReader.Imaging.GIFDecoder.Decode(Application.GetResourceStream(new Uri("Files/Books/" + item.BookId + "/" + item.Text, UriKind.Relative)).Stream); 
    image.Source = gif.Frames[0].Image; 
} 
else 
{ 
    var img = new BitmapImage(new Uri("/Files/Books/" + item.BookId + "/" + item.Text, UriKind.Relative)); 
    image.Source = img; 
} 

image.SetValue(Canvas.LeftProperty, (double)item.LocationX); 
image.SetValue(Canvas.TopProperty, (double)(Application.Current.Host.Content.ActualHeight - item.LocationY)); 

image.SizeChanged += (o, e) => 
{ 
    var sender = o as ExtendedImage; 
    image.SetValue(UIElement.RenderTransformOriginProperty, new Point(0.5, 0.5)); 
}; 

image.InvalidateMeasure(); 

if (!item.Visible) 
{ 
    image.Visibility = System.Windows.Visibility.Collapsed; 
} 

ContentPanel.Children.Add(image); 

回答

1

如果Image的容器具有HeightWidth,图像应该有它。尝试将您的Image放入GridStackPanel并明确设置大小。

+0

它包含在画布中,并且画布具有高度和宽度。 –

+0

感谢您的回答。 Canvas容器的大小是NaN。当我修复这个问题时,我的问题就解决了。 –

+0

你能告诉我你是如何解决你的问题的,我也有同样的问题。 –

3

在渲染之前,您无法访问图像的大小。如果没有明确的尺寸,则无法渲染Canvas中的项目。

所以你的问题是,你正试图获得图像的大小之前,它实际上呈现。

相关问题