How can I set a StackPanels margins individually?

巧了我就是萌 提交于 2019-12-08 06:23:46

问题


I can set the margin of a stackpanel in code-behind like this:

StackPanel sp2 = new StackPanel();
sp2.Margin = new System.Windows.Thickness(5);

But how can I set each individually, both of these don't work:

PSEUDO-CODE:

sp2.Margin = new System.Windows.Thickness("5 0 0 0");
sp2.Margin.Left = new System.Windows.Thickness(5);

回答1:


You can also try this:

sp2.Margin = new System.Windows.Thickness{ Left = 5 };



回答2:


Margin is of type Thickness which is a structure.

The parsing of "5 0 0 0" is a XAML thing, its not something that Thickness constructor handles.

Use

sp2.Margin = new System.Windows.Thickness(5,0,0,0);

since Thickness is a structure this should also work, leaving the other margin values unmodified:-

sp2.Margin.Left = 5;


来源:https://stackoverflow.com/questions/1194447/how-can-i-set-a-stackpanels-margins-individually

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!