C# 在代码中设置边距属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1003772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:18:16  来源:igfitidea点击:

Setting Margin Properties in code

c#wpfmargin

提问by Giffyguy

MyControl.Margin.Left = 10;

Error:

错误:

Cannot modify the return value of 'System.Windows.FrameworkElement.Margin' because it is not a variable

无法修改“System.Windows.FrameworkElement.Margin”的返回值,因为它不是变量

采纳答案by Jon Skeet

The problem is that Marginis a property, and its type (Thickness) is a value type. That means when you access the property you're getting a copyof the value back.

问题是它Margin是一个属性,它的类型 ( Thickness) 是一个值类型。这意味着当您访问该属性时,您将获得该值的副本

Even though you canchange the value of the Thickness.Leftproperty for a particular value (grr... mutable value types shouldn't exist), it wouldn't change the margin.

即使您可以更改Thickness.Left特定值的属性值(grr...可变值类型不应该存在),它也不会更改边距。

Instead, you'll need to set the Marginproperty to a new value. For instance (coincidentally the same code as Marc wrote):

相反,您需要将该Margin属性设置为新值。例如(巧合的是与 Marc 写的代码相同):

Thickness margin = MyControl.Margin;
margin.Left = 10;
MyControl.Margin = margin;

As a note for library design, I would have vastly preferred it if Thicknesswere immutable, but with methods that returned a new value which was a copy of the original, but with one part replaced. Then you could write:

作为库设计的注意事项,如果它Thickness是不可变的,我会非常喜欢它,但是方法返回一个新值,该值是原始值的副本,但替换了一部分。然后你可以写:

MyControl.Margin = MyControl.Margin.WithLeft(10);

No worrying about odd behaviour of mutable value types, nice and readable, all one expression...

不用担心可变值类型的奇怪行为,美观且可读,所有的表达式...

回答by Ash

One would guess that (and my WPF is a little rusty right now) that Margin takes an object and cannot be directly changed.

有人会猜测(我的 WPF 现在有点生疏)Margin 接受一个对象并且不能直接更改。

e.g

例如

MyControl.Margin = new Margin(10,0,0,0);

回答by Marc Gravell

Marginis returning a struct, which means that you are editing a copy. You will need something like:

Margin正在返回一个结构体,这意味着您正在编辑一个副本。你将需要类似的东西:

var margin = MyControl.Margin;
margin.Left = 10;
MyControl.Margin = margin;

回答by Guffa

The Marginproperty returns a Thicknessstructure, of which Leftis a property. What the statement does is copying the structure value from the Marginproperty and setting the Leftproperty value on the copy. You get an error because the value that you set will not be stored back into the Marginproperty.

Margin属性返回一个Thickness结构,其中Left是一个属性。该语句的作用是从Margin属性复制结构值并Left在副本上设置属性值。您会收到错误消息,因为您设置的值不会存储回Margin属性中。

(Earlier versions of C# would just let you do it without complaining, causing a lot of questions in newsgroups and forums on why a statement like that had no effect at all...)

(早期版本的 C# 只会让你毫无怨言地去做,在新闻组和论坛中引发了很多关于为什么这样的声明根本没有效果的问题......)

To set the property you would need to get the Thicknessstructure from the Marginproperty, set the value and store it back:

要设置属性,您需要ThicknessMargin属性中获取结构,设置值并将其存储回来:

Thickness m = MyControl.Margin;
m.Left = 10;
MyControl.Margin = m;

If you are going to set all the margins, just create a Thicknessstructure and set them all at once:

如果您要设置所有边距,只需创建一个Thickness结构并一次设置它们:

MyControl.Margin = new Thickness(10, 10, 10, 10);

回答by Daler Tursunov

To use Thicknessyou need to create/change your project .NET frameworkplatform version to 4.5. becaus this method available only in version 4.5. (Also you can just download PresentationFramework.dll and give referense to this dll, without create/change your .NET frameworkversion to 4.5.)

要使用,Thickness您需要将项目.NET framework平台版本创建/更改为 4.5。因为此方法仅在 4.5 版中可用。(您也可以只下载 PresentationFramework.dll 并引用此 dll,而无需创建/将您的.NET framework版本更改为 4.5。)

But if you want to do this simple, You can use this code:

但是如果你想做这个简单的,你可以使用这个代码:

MyControl.Margin = new Padding(int left, int top, int right, int bottom);

also

MyControl.Margin = new Padding(int all);

This is simple and no needs any changes to your project

这很简单,不需要对您的项目进行任何更改

回答by Jan Turoň

It's a bit unclear what are you asking, but to make things comfortable, you can inherit your own Control and add a property with the code that Marc suggests:

有点不清楚你在问什么,但为了让事情变得舒服,你可以继承你自己的 Control 并使用 Marc 建议的代码添加一个属性:

class MyImage : Image {
    private Thickness thickness;
    public double MarginLeft {
        get { return Margin.Left; }
        set { thickness = Margin; thickness.Left = value; Margin = thickness; }
    }
}

Then in the client code you can write just

然后在客户端代码中你可以只写

MyImage img = new MyImage();
img.MarginLeft = 10;
MessageBox.Show(img.Margin.Left.ToString()); // or img.MarginLeft

回答by LuckyLikey

One could simply use this

人们可以简单地使用这个

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

回答by Rishi

Depends on the situation, you can also try using padding property here...

根据情况,您也可以尝试在此处使用 padding 属性...

MyControl.Margin=new Padding(0,0,0,0);

回答by KalleP

Margin = new Thickness(0, 0, 0, 0);

边距 = 新厚度(0, 0, 0, 0);