我有一个带有背景图像的窗口。图像可能会在运行时更改,但这实际上不应该影响到它。
我想让图像固定在左上角(已经是这样了),并且不缩放(也是正确的)。但当窗口的大小大于图像时,我需要图像重复(平铺)显示。我正在做...
我缺少什么?
先行感谢!
我想让图像固定在左上角(已经是这样了),并且不缩放(也是正确的)。但当窗口的大小大于图像时,我需要图像重复(平铺)显示。我正在做...
我缺少什么?
先行感谢!
你需要设置TileMode
属性,以及Viewport
和ViewportUnits
属性:
例如:
<Window.Background>
<ImageBrush ImageSource="myImage.png"
Viewport="0,0,300,300"
ViewportUnits="Absolute"
TileMode="Tile"
Stretch="None"
AlignmentX="Left"
AlignmentY="Top" />
</Window.Background>
注意:Viewport属性的第二个和第三个部分指示每次重复的所需大小。如果您想要显示整个图像,则应该是图像的宽度和高度。<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
}
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;