Skip to content

WPF问题记录

约 169 字小于 1 分钟

WPF

2024-09-24

Img图片大小问题

 <Grid>
     <Canvas x:Name="canvas" Background="Transparent" Panel.ZIndex="99" 
             Width="{Binding ElementName=img,Path=ActualWidth}" Height="{Binding ElementName=img,Path=ActualHeight}">
         <Rectangle Name="selectionRectangle" Stroke="Red" StrokeThickness="2" Visibility="Collapsed"/>
     </Canvas>
     <Image x:Name="img" Source="{Binding SlideInfoSelectedItem.LocatePath}" /><!--Source="{Binding SlideInfoSelectedItem.LocatePath}"-->
     <!---->
 </Grid>

代码如上 如果想通过img的ActualWidth来设置canvas的大小, 行不通,后台获取的也是上一次的实际图片的大小,只有通过Width="{Binding ElementName=img,Path=ActualWidth}" Height="{Binding ElementName=img,Path=ActualHeight}"的形式; 但是依然获取不到图片的大小,解决笨办法: img.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/images.jpg", UriKind.Absolute)); //给个默认位置 不然获取不到大小