简体   繁体   English

如何在画布上保持我的按钮位置相同?

[英]How do I keep the positions of my Buttons the same on a Canvas?

I have a WPF Window that has a Image in the background as a canvas. 我有一个WPF窗口,在后台有一个Image作为画布。 It is a picture of a pirate ship. 这是一艘海盗船的照片。 I want to have buttons attached to certain points on the image which stay the same even if the size of the window is altered. 我希望按钮附加到图像上的某些点,即使窗口的大小发生变化也保持不变。 With a fixed Window size this is fine. 使用固定的窗口大小,这很好。 Can I select Hotspots at all where my Buttons will stay in touch with the same position on the image? 我可以选择热点,我的按钮会与图像上的相同位置保持联系吗? Or do I use percentage positioning for the buttons? 或者我使用按钮的百分比定位?

This may be a lot to ask of WPF. 这对WPF来说可能有很多要求。

The buttons are on top of the canvas. 按钮位于画布顶部。

Thanks 谢谢

Assuming your Background Image stretches according to Window Size, I'd use a Converter and set Canvas.Top and Canvas.Left based on a percentage. 假设您的背景图像根据窗口大小拉伸,我将使用Converter并根据百分比设置Canvas.TopCanvas.Left

You could either code your own IValueConverter that accepts the Canvas.Height and Canvas.Width , and a ConverterParameter identifying what percentage you want of the value, or I have a fairly generic Math Converter on my blog that you are free to use if you prefer. 您可以编写自己的IValueConverter接受Canvas.HeightCanvas.Width ,以及ConverterParameter识别您想要的值百分比,或者我在博客上有一个相当通用的Math Converter,如果您愿意,可以自由使用。

<Button Canvas.Top="{Binding ElementName=MyCanvas, Path=ActualHeight, Converter={StaticResource MathConverter}, ConverterParameter=@VALUE*.25}"
        Canvas.Left="{Binding ElementName=MyCanvas, Path=ActualWith, Converter={StaticResource MathConverter}, ConverterParameter=@VALUE*.25}" 
        ... />

(I forget if Height / Width or ActualHeight / ActualWidth is the more accurate value - you may need to test that one) (我忘了如果Height / WidthActualHeight / ActualWidth是更准确的值 - 你可能需要测试那个)

You could take a common ancestor of the canvas and buttons, give it a fixed size, and wrap it in a Viewbox . 你可以采用画布和按钮的共同祖先,给它一个固定的大小,并将其包装在Viewbox Then you can let the Viewbox take care of scaling for you. 然后,您可以让Viewbox为您进行缩放。 The entire scene--buttons, image, and anything else--will be scaled together, so you won't need to manually adjust any positioning. 整个场景 - 按钮,图像和其他任何内容 - 将被缩放在一起,因此您无需手动调整任何定位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在视图模型中获取鼠标位置 - How do I get mouse positions in my view model 随着项目尺寸的增加或缩小,如何在XAML的Canvas上将项目保持在同一位置? - How do you keep an item at the same point on a Canvas in XAML as it grows or shrinks in size? 如何将组合框IsChecked绑定到同一组的4个按钮? - How do I bind combobox IsChecked to 4 buttons from the same group? 在将多个UIElements添加到画布时,如何保持UI响应? - How do I keep the UI responsive when adding many UIElements to a canvas? 当鼠标悬停在WPF按钮模板上时,如何防止其更改所有按钮? - How do I keep WPF Button Template from changing All Buttons When Moused Over? 如何设置按钮样式 - How do I style Buttons 如何将插入符号移动到WPF RichTextBox中的一定数量的位置? - How do I move the caret a certain number of positions in a WPF RichTextBox? 获取所有黑色像素及其在画布上的位置? - Getting all black pixels with its positions for my canvas? WPF DataGrid:如何使DataGridTemplateColumn中的所有按钮具有相同的图像? - WPF DataGrid: How do I get all buttons in a DataGridTemplateColumn to have the same image? 如何为画布存储非持久性图像数组以获取背景? - How do I store a non-persistent array of images for my canvas to get backgrounds from?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM