简体   繁体   English

调整usercontrol大小时,包含图像图像的WPF userControl不会调整大小

[英]WPF userControl containing an image image doesnt resize when usercontrol is resized

I am working in wpf. 我在wpf工作。 I have a userControl in which sits an Image control. 我有一个userControl,其中有一个Image控件。

I add a BitmapImage to this userControl through image.source. 我通过image.source向这个userControl添加了一个BitmapImage。

This userControl is then added to a canvas, any control attached to this canvas has adorners attached so that each of the four corners can be dragged to resize the userControl. 然后将此userControl添加到画布,附加到此画布的任何控件都附加了装饰器,以便可以拖动四个角中的每个角来调整userControl的大小。

My problem is, is that the bitmap doesnt resize with the userControl. 我的问题是,位图没有使用userControl调整大小。

Is there an easy way to make the bitmap redraw when the userControl resizes? 当userControl调整大小时,是否有一种简单的方法可以重新绘制位图?

Here is the XAML for the user control: 以下是用户控件的XAML:

<UserControl x:Name="cusImageControl" x:Class="StoryboardTool.CustomImage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" BorderThickness="0" MouseDown="cusImageControl_MouseDown">
    <Image x:Name="image"  >
        <Image.ContextMenu>
            <ContextMenu>
                <MenuItem x:Name="ContextMenuBringForward" Header="BringForward" Click="ContextMenuBringForward_Click"/>
                <MenuItem x:Name="ContextMenuSendBackward" Header="SendBackward" Click="ContextMenuSendBackward_Click"/>
            </ContextMenu>
        </Image.ContextMenu>
    </Image>
</UserControl>

public void chooseImage()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Choose Image to Add";

            if (ofd.ShowDialog() == true)
            {
                BitmapImage bImage = new BitmapImage();
                bImage.BeginInit();
                bImage.UriSource = new Uri(ofd.FileName);
                bImage.EndInit();

                image.Width = bImage.Width;
                image.Height = bImage.Height;
                image.Source = bImage;
                //image.Stretch = Stretch.Fill;
            }
        }

You seem to be setting a width and height in your code behind... this is the size that the Image will be. 您似乎在后面的代码中设置宽度和高度...这是Image的大小。 Instead of this, try setting the Image.Source property in xaml: 而不是这样,尝试在xaml中设置Image.Source属性:

<UserControl x:Class="WpfApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Image Source="/WpfApplication1;component/Images/ImageName.png" />
    </Grid>
</UserControl>

If I put this into MainWindow.xaml , the Image resizes when resizing the Window. 如果我把它放到MainWindow.xamlImage在调整窗口大小时调整大小。

If you need to set the URL of the Image in code, then you can add a property in your view model, bind it to the Source property and change that to the new path in code instead: 如果需要在代码中设置Image的URL,则可以在视图模型中添加属性,将其绑定到Source属性并将其更改为代码中的新路径:

<Image Source="{Binding ViewModelSourceProperty}" /> // don't set size here

I hope that helps. 我希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM