WPF窗口背景的ImageBrush不平铺 [英] WPF Window Background ImageBrush not tiling

查看:1942
本文介绍了WPF窗口背景的ImageBrush不平铺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个背景图像的窗口。图像可以在运行时改变它真的不应该为这件事情。

I have a window with a background image. The image may change at runtime which really should not matter for this.

我想要的图像被固定到左上(它是),而不是比例(这也是正确的。但是,我需要的图像重复(瓦)时所取得的窗口比图像大。我做...

I want the image to be fixed to the top left (which it is) and not scale (which is also correct. But I need the image to repeat (tile) when the window is made larger than the image. I am doing ...


        
    

我想什么?

TIA

推荐答案

您需要设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.media.tilebrush.tilemode.aspx\"><$c$c>TileMode财产所有权以及在视窗 ViewportUnits

例如:

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

注:视口的第二部分2 属性指示每个重复所需的大小。如果要显示的整个图像,这些应该是图像的宽度和高度。

Note: the second 2 segments of the Viewport attribute indicate the desired size of each repetition. If you want to display the entire image, these should be the width and height of the image.

输出示例:

Example output:

编辑回应评论

如果你不知道图像的大小在视窗属性来指定,可以使用绑定的IValueConverter 从图像计算。我相信一定有这样做的更好的办法,但我还没有找到一个呢!

If you don't know the size of the image to specify in the Viewport property, you can use a Binding with an IValueConverter to calculate it from the image. I am convinced there must be a better way of doing this, but I haven't found one yet!

XAML:

<Window.Resources>
    <local:Converter x:Key="Converter" />
</Window.Resources>
<Window.Background>

    <ImageBrush ImageSource="myImage.png"
    ViewportUnits="Absolute"
    TileMode="Tile"
    Stretch="None"
    AlignmentX="Left"
    AlignmentY="Top" 
    Viewport="{Binding ImageSource, RelativeSource={RelativeSource Self}, Converter={StaticResource Converter}}"/>
</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
}

这篇关于WPF窗口背景的ImageBrush不平铺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆