【发布时间】:2017-12-06 16:18:37
【问题描述】:
我已经为 WPF 创建了自己的控件,我想在 .cs 代码中定义更改边距参数的方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public String TagName { get; set; }
public UserControl1(String TagName)
{
this.TagName = TagName;
InitializeComponent();
}
public double TagValue { get; set; }
public void test(double val) {
valueRectangle.Margin.Top(10);
}
}
}
但是在将任何值设置为 Margin.Top 时我仍然遇到错误
编辑:此处的错误代码:
严重性代码描述项目文件行抑制状态 错误 CS1955 不可调用的成员 'Thickness.Top' 不能像方法一样使用。 WpfApplication1 X:\07_projects\00_ostatni\CSharp\WpfApplication1\WpfApplication1\UserControl1.xaml.cs 36 活动
【问题讨论】:
-
你的错误是
Cannot modify the return value of 'System.Windows.FrameworkElement.Margin' because it is not a variable吗? -
除了
Top是一个属性,而不是一个方法,你应该分配一个新的厚度,比如valueRectangle.Margin = new Thickness(0, 10, 0, 0); -
阅读 Jon Skeets 的回答,了解为什么需要分配新的保证金。 stackoverflow.com/questions/1003772/…??
-
谢谢,就是这样。我将使用
new Thickness()。