Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF窗口背景图像笔刷未平铺_Wpf_Background_Window - Fatal编程技术网

WPF窗口背景图像笔刷未平铺

WPF窗口背景图像笔刷未平铺,wpf,background,window,Wpf,Background,Window,我有一个带有背景图像的窗口。映像可能会在运行时发生更改,这实际上与此无关 我希望图像固定在左上角(它是),而不是缩放(这也是正确的)。但当窗口比图像大时,我需要图像重复(平铺)。我正在做 我错过了什么 TIA您需要设置属性以及视口和视口单元: 例如: <Window.Background> <ImageBrush ImageSource="myImage.png" Viewport="0,0,300,300" ViewportUnit

我有一个带有背景图像的窗口。映像可能会在运行时发生更改,这实际上与此无关

我希望图像固定在左上角(它是),而不是缩放(这也是正确的)。但当窗口比图像大时,我需要图像重复(平铺)。我正在做

我错过了什么

TIA

您需要设置属性以及
视口
视口单元

例如:

<Window.Background>
    <ImageBrush ImageSource="myImage.png"
        Viewport="0,0,300,300"
        ViewportUnits="Absolute"
        TileMode="Tile"
        Stretch="None"
        AlignmentX="Left"
        AlignmentY="Top"  />
</Window.Background>
值转换器:

public class Converter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var source = (ImageSource)value;
        return new Rect(0,0,source.Width, source.Height);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

如果您希望整个解决方案使用c语言#


我是,但事实并非如此work@Jeff-我以前没有见过,但您需要设置视口和视口单位-我已更新答案以反映这一点(+1)比我的答案更好,仍然使用ImageBrush。好的,谢谢-这就像你描述的那样,但图像可能会改变。有没有办法将视口设置设置为图像的实际大小?你只能使用绝对视口单位(如上所述)或相对于容器的单位(在本例中为
窗口
)。例如,如果希望图像始终占据窗口大小的50%,可以删除
Stretch=“None”
ViewportUnits=“Absolute”
属性,并将
视口
值更改为
0,0,0.5,0.5
。我认为不可能使其与图像大小相关。我知道这个问题很老。只需发布一个可能的后端解决方案。
public class Converter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var source = (ImageSource)value;
        return new Rect(0,0,source.Width, source.Height);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(@"c:\your\image\source.gif"));
brush.TileMode = TileMode.Tile;
brush.ViewportUnits = BrushMappingMode.Absolute;
brush.Viewport = new Rect(0, 0, brush.ImageSource.Width, brush.ImageSource.Height);

MainWindow1.Background = brush;