内部图像的WPF用户控件不会调整大小

时间:2012-12-13 08:20:36

标签: wpf image user-controls resize

我有一个仅包含图像的用户控件。该图像在执行时间内计算。在后面有一个WritableBitMap,我在那里进行绘图。然后将image.source设置为此WritableBitMap。 所以我的用户控件如下:

public partial class ColorBrainMap : UserControl
{  
    //... some stuff
    //I load the base image from the resources
    BitmapImage tempImg = new BitmapImage();
    tempImg.BeginInit();
    tempImg.UriSource = new Uri("pack://application:,,,/ColorBrainMap;component/imgs/brain.png");
    tempImg.EndInit();

    //I copy the image into a WritableBitMap for convenience
    WriteableBitmap baseImage = new WriteableBitmap(tempImg);
    //I do some plotting over the base image
    //and set the final result as a source for the image
    image.Source = baseImage;
 }

另一方面,我的xaml非常简单:

<Grid>
        <Image HorizontalAlignment="Stretch" Name="image" Stretch="Fill" VerticalAlignment="Stretch"/>
</Grid>

我创建了我的新项目。我添加组件作为参考,并将一个新实例放入我的xaml中,看起来像这样:

<Window x:Class bla bla 
    Height="396" Width="541" 
    xmlns:my="clr-namespace:ColorBrainMapTOOL;assembly=ColorBrainMap">
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="300*" />
        <ColumnDefinition Width="219*" />
      </Grid.ColumnDefinitions>
      <my:ColorBrainMap HorizontalAlignment="Stretch" Name="brainMap" VerticalAlignment="Stretch" />
      <!-- some other components in the other column of the grid-->
      </Grid>
</Window>

在我尝试调整大小之前,整个过程都很有效。控件本身调整大小(我测试它添加一些背景和边框,整个事情移动nicelly)但不是图像。图像根据需要重新计算图表并且运行良好,但它会坚持初始大小。

如何在不调整内部图像大小并重新计算图形和所有工作的情况下如何做到这一点的任何线索?

2 个答案:

答案 0 :(得分:1)

我认为你可以使用ViewBox并设置你的图片或ColorBrainMap作为它的孩子;

答案 1 :(得分:0)

您可以尝试绑定:

<Grid>
    <Image Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Grid}}, Path=ActualHeight}" Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Grid}}, Path=ActualWidth}" Name="image" Stretch="Fill">
</Grid>