如何更改wpf中组件的属性?
这是一个非常简单的例子
我想点击按钮,然后改变它本身的位置,
Visual Studio提示我这是一个公共属性,并且类型是double。为什么我无法更改该值?而且它没有提供任何方法让我更改顶部属性,那么我如何更改该属性呢?
<Button Content="Button" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="0,0,0,0" Name="Button1" VerticalAlignment="Top" Width="75" Grid.Row="1" />
MsgBox(Button1.Margin.Top)
Button1.Margin.Top = 10
This is a very simple case
I want to click on the button, then change the itself position
the visual studio prompt me that is a public property, and the type is double. Why I cannot change the value? And it does not provide any method let me change the top property, so how I can change the property?
<Button Content="Button" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="0,0,0,0" Name="Button1" VerticalAlignment="Top" Width="75" Grid.Row="1" />
MsgBox(Button1.Margin.Top)
Button1.Margin.Top = 10
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法单独设置每个边距,但可以将按钮边距设置为新的厚度,并将 10 硬编码为上边距,同时保持其他值不变:
You can't set each margin individually, but you can set the button margin to a new thickness and hardcode 10 as the top margin while leaving the other values untouched:
如果您想移动按钮,请不要使用边距,它不是为此目的而设计的。
相反,将按钮放在 Canvas 中,然后您可以设置 Canvas.Top/Bottom/Left/Right 来移动按钮(它们是附加属性)。
If you want to move the button around don't use Margin, it's not made for that intent.
Instead, place your button in a Canvas, then you can set Canvas.Top/Bottom/Left/Right to move your button around (they are attached properties).