如何在数据绑定的代码中创建平铺图像? [英] How can I create a tiled image in code that is also data-bound?
问题描述
image myImage = new Image();
myImage.Stretch = Stretch.UniformToFill;
myImage.SetBinding(Image.SourceProperty,myBinding);
问题是我要平铺此图像,所以我可以找到这样做的唯一方法是创建一个ImageBrush并设置TileMode属性。但是没有SetBinding功能,所以我该如何完成我需要的?
ImageBrush myBrush = new ImageBrush();
myBrush.TileMode = TileMode.Tile;
//不能这样做!
myBrush.SetBinding(ImageBrush.SourceImageProperty,myBinding);
有没有其他方法在代码隐藏中平铺这样的图像?
您不需要更改任何东西,只能使用BindingOperations:
code> BindingOperations.SetBinding(myBrush,ImageBrush.ImageSourceProperty,myBinding);
您需要定义视口并用画笔填充视口:
MyBrush.Viewport = new Rect(0,0,0.1,0.1);
//创建一个矩形并用ImageBrush绘制。
Rectangle rec = new Rectangle();
rec.Stroke = Brushes.LimeGreen;
rec.StrokeThickness = 1;
rec.Fill = MyBrush;
Current in code-behind, I dynamically create a WPF Image control and bind the source to a custom databinding. This will eventually be added to a grid to provide a background image:
Image myImage = new Image();
myImage.Stretch = Stretch.UniformToFill;
myImage.SetBinding(Image.SourceProperty, myBinding);
The problem is that I want to tile this image, so the only way I can find to do this is to create an ImageBrush and set the TileMode property. But there is no "SetBinding" function, so how can I accomplish what I need?
ImageBrush myBrush = new ImageBrush();
myBrush.TileMode = TileMode.Tile;
// Can't do this!
myBrush.SetBinding(ImageBrush.SourceImageProperty, myBinding);
Are there any other ways to tile an image like this in code-behind?
You need not to change anything but use the BindingOperations:
BindingOperations.SetBinding(myBrush, ImageBrush.ImageSourceProperty, myBinding);
And you need to define the Viewport and the fill the viewport with brush:
MyBrush.Viewport = new Rect(0, 0, 0.1, 0.1);
// Create a rectangle and paint it with the ImageBrush.
Rectangle rec = new Rectangle();
rec.Stroke = Brushes.LimeGreen;
rec.StrokeThickness = 1;
rec.Fill = MyBrush;
这篇关于如何在数据绑定的代码中创建平铺图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!