0

I am displaying lot of custom data types (implemented as structs internally) on my UI. I was hoping to use Fody.PropertyChanged but it seems it works only with classes and not structs.

A sample code:

namespace FodyQuestion
{
    [ImplementPropertyChanged]
    public struct FodyStruct
    {
        public int MyProperty { get; set; }
    }
}

And the error I see is:

Error   1   Attribute 'ImplementPropertyChanged' is not valid on this declaration type. It is only valid on 'class' declarations.

Has anyone been able to do this with some workarounds? I am willing to check out the source code of Fody and make changes if needed. Any pointers?

NotAgain
  • 1,927
  • 3
  • 26
  • 42
  • 1
    In order to get notified about property changes, the WPF binding infrastructure needs to hold a reference to the binding source object, which won't work with struct types. So with regard to data binding your question doesn't make much sense. – Clemens Sep 01 '14 at 05:24
  • Mutable value types (structs) is something you should avoid. You can find several questions about this on Stack Overflow. And as Clemens has pointed out you cannot data bind to a value type. Why do you want to use value types in the first place? – Martin Liversage Sep 01 '14 at 06:05
  • @MartinLiversage: I have created custom Data Types to represent units, something like Km/hr. These are rather light weight with very few fields. And I have to support the collection of such data types also. So I thought it will be a good candidate to be a struct to keep the overheads down. – NotAgain Sep 01 '14 at 06:27
  • 1
    @NotAgain: They are perfect candidates for value types, but why do you want to make them mutable? Ints and strings are not mutable - you just assign a new value if you want to make change. You should be able to apply the same logic to your domain. – Martin Liversage Sep 01 '14 at 06:36

0 Answers0