English 中文(简体)
我怎样才能在代码中创建一个也包含数据的繁忙图像呢?
原标题:How can I create a tiled image in code that is also data-bound?

在当前代码后面, 我动态地创建了 WPF 图像控制, 并将源与自定义数据绑在一起。 这最终会添加到网格中, 以提供一个背景图像 :

Image myImage = new Image();
myImage.Stretch = Stretch.UniformToFill;
myImage.SetBinding(Image.SourceProperty, myBinding);

问题在于我想将这张图像粘贴在一块上, 所以我唯一能找到的方法就是创建图像笔迹, 并设置天线图属性。 但是没有“ etBinding ” 功能, 因此我怎样才能完成我所需要的呢?

ImageBrush myBrush = new ImageBrush();
myBrush.TileMode = TileMode.Tile;
// Can t do this!
myBrush.SetBinding(ImageBrush.SourceImageProperty, myBinding);

是否有其他方法 将这样的图像在代码后方打成瓷砖?

最佳回答

您不需要更改任何东西,但使用“装订操作 ” :

BindingOperations.SetBinding(myBrush, ImageBrush.ImageSourceProperty, myBinding);

您需要定义“ Viewport” 和“ 刷子” 来填充“ Viewport ” :

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;
问题回答

我尝试了以下方法。 在调试模式中, 视觉笔刷的属性设置正确 。 当然图像显示为拉伸的图像 。 不知道原因 。 希望它有用 。

财产

        public TileMode Mode { get; set; }

约束性

        VisualBrush myBrush = new VisualBrush();

        Uri uri = new Uri("picture.png", UriKind.RelativeOrAbsolute);
        ImageSource src = new BitmapImage(uri);
        myBrush.Visual = new Image() { Source = src };

        this.Mode = TileMode.Tile;

        Binding bind = new Binding() { Source = Mode };
        BindingOperations.SetBinding(myBrush, VisualBrush.TileModeProperty, bind);

        this.Background = myBrush;

I don t like code behind, so it s hard to me to write code-behind sample quickly.
Here s markup sample:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <Grid.Background>
        <ImageBrush ImageSource="Sample.jpg" TileMode="Tile" Viewport="0,0,0.5,0.5"/>
    </Grid.Background>
</Grid>

而不是硬码图像 (imageSource="Sample.jpg" ), 您可以写入任何像这样的绑定表达式 : imageSource="{inding myBackbackbroundImagieUri}"





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签