WPF Window Background ImageBrush not tiling

后端 未结 2 515
轻奢々
轻奢々 2021-02-18 23:12

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

2条回答
  •  执笔经年
    2021-02-19 00:01

    You need to set the TileMode property as well as the Viewport and ViewportUnits:

    For example:

    
        
    
    

    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: tiled magnifiers

    Edit in response to comments

    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:

    
        
    
    
    
        
    
    

    Value converter:

    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
    }
    

提交回复
热议问题