2011-10-02 58 views
0

这是一个非常简单的例子如何更改wpf中组件的属性?

我想点击该按钮,然后改变自身位置

在Visual Studio提示我这是一个公共属性,类型为双。为什么我不能改变价值?它并没有提供任何方法让我更改顶级属性,所以我如何更改属性?

<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 
+0

通过乔恩斯基特在这里找到答案: http://stackoverflow.com/questions/1003772/whats-the-problem-with-this-code –

回答

1

不能设置每个单独保证金,但你可以在按键边距设置为一个新的厚度和硬编码10顶边,同时保持其他值不变:

Button1.Margin = New Thickness(Button1.Margin.Left, 10, Button1.Margin.Right, Button1.Margin.Bottom) 
0

如果您希望移动的按钮,不使用保证金,这不是为意图制造。

取而代之,将您的按钮放置在画布中,然后您可以设置Canvas.Top/Bottom/Left/Right来移动您的按钮(它们是附加属性)。