开发者

Binding Size of Canvas Background

开发者 https://www.devze.com 2023-02-14 02:53 出处:网络
I have a canvas 开发者_开发技巧that needs a graphical background using custom images of various sizes, but I want to bind the height and width of the background image to some properties somewhere. rig

I have a canvas 开发者_开发技巧that needs a graphical background using custom images of various sizes, but I want to bind the height and width of the background image to some properties somewhere. right now I have an image displaying as background but its size is fixed. It turns out that the width and height property of the image and of the imagebrush is readonly. Is there anyway to bind the width and height?

p.s I have tried viewport, but it uses a rect object and its width and height are not dependency properties. Then I tried to use one tile of the viewport and put it in a rectangle then I can resize the rectangle to achieve what I want but I cannot figure out how to put a rectangle as the canvas's background. Any Ideas? Thanks in advance.


An easy way is to use a VisualBrush as the Background for the Canvas. Set Stretch to None and then bind the Width and Height of the Image in the Visual

<Canvas ...>
    <Canvas.Background>
        <VisualBrush Stretch="None">
            <VisualBrush.Visual>
                <Image Source="SomeSource"
                       Stretch="Fill"
                       Width="{Binding ...}"
                       Height="{Binding ...}"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </Canvas.Background>
    <!--...-->
</Canvas>


Can't say that i managed to follow what you're trying to do exactly, but you can use VisualBrush to put the Rectangle as the background of the canvas.

I'm somewhat sure that what you're doing can be solved easily - but I couldn't really understand what it is you're trying to do.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号