【问题标题】:WPF userControl containing an image image doesnt resize when usercontrol is resized调整用户控件大小时,包含图像图像的 WPF 用户控件不会调整大小
【发布时间】:2013-07-19 13:43:56
【问题描述】:

我在 wpf 工作。我有一个 userControl,其中有一个 Image 控件。

我通过 image.source 向这个 userControl 添加了一个 BitmapImage。

然后将此 userControl 添加到画布中,附加到此画布的任何控件都附加了装饰器,以便可以拖动四个角中的每一个来调整 userControl 的大小。

我的问题是,位图不使用 userControl 调整大小。

当用户控件调整大小时,有没有一种简单的方法可以使位图重绘?

这是用户控件的 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;
            }
        }

【问题讨论】:

  • 你有没有提到用户控件中的尺寸(宽度,高度)?
  • 你能告诉我们你的 XAML 代码吗?
  • 为 UserControl 添加了 XAML,以及我选择图像的方法。
  • 图像缩放模式是什么?例如:

标签: c# wpf image visual-studio adorner


【解决方案1】:

您似乎在后面的代码中设置了宽度和高度...这是Image 的大小。而不是这个,尝试在 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>

如果我把它放到MainWindow.xaml 中,Image 会在调整窗口大小时调整大小。

如果您需要在代码中设置 Image 的 URL,那么您可以在视图模型中添加一个属性,将其绑定到 Source 属性,然后将其更改为代码中的新路径:

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    相关资源
    最近更新 更多