0

I have created my own control for WPF and in .cs code I want to define method to change margin parameter.

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);
        }
    }
}

But I'm still getting error while setting any value to Margin.Top

Edit: Errorcode here:

Severity Code Description Project File Line Suppression State Error CS1955 Non-invocable member 'Thickness.Top' cannot be used like a method. WpfApplication1 X:\07_projects\00_ostatni\CSharp\WpfApplication1\WpfApplication1\UserControl1.xaml.cs 36 Active

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Ondřej Michejda
  • 138
  • 1
  • 12
  • 1
    Is your error `Cannot modify the return value of 'System.Windows.FrameworkElement.Margin' because it is not a variable` ? – Stuart Jul 03 '17 at 11:52
  • 1
    Besides that `Top` is a property, not a method, you should assign a new Thickness like `valueRectangle.Margin = new Thickness(0, 10, 0, 0);` – Clemens Jul 03 '17 at 11:55
  • 1
    Read Jon Skeets answer here for why you need to asiggn a new Margin. https://stackoverflow.com/questions/1003772/setting-margin-properties-in-code ?? – Stuart Jul 03 '17 at 11:57
  • Thanks, that's it. I will use `new Thickness()`. – Ondřej Michejda Jul 03 '17 at 11:58

1 Answers1

1

As Clemens wrote:

Besides that Top is a property, not a method, you should assign a new Thickness like valueRectangle.Margin = new Thickness(0, 10, 0, 0);

Ondřej Michejda
  • 138
  • 1
  • 12