【问题标题】:How to change rectangle property in its .cs code?如何更改其 .cs 代码中的矩形属性?
【发布时间】: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()

标签: c# wpf


【解决方案1】:

正如克莱门斯所写:

除了 Top 是一个属性,而不是一个方法,你应该分配一个新的 厚度类似 valueRectangle.Margin = new Thickness(0, 10, 0, 0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 2017-11-20
    • 2020-07-16
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多