如何在数据绑定的代码中创建平铺图像? [英] How can I create a tiled image in code that is also data-bound?

查看:135
本文介绍了如何在数据绑定的代码中创建平铺图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在代码隐藏的当前,我动态创建一个WPF图像控件,并将源绑定到自定义数据绑定。这将最终添加到网格中以提供背景图像:

  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屋!

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