1

I have an Entry class, through which i want to expose data to a Gridview in WPF (via an List<Entry>). I need to have a column in the Gridview for every property of the Entry object (also get one column for every entry of propsA3), but I am uncertain how to define the getter/setter methods for the array in order to always get/set the properties of the underlying data.

public Entry
{
  private ObjA oA;
  private ObjB[] listB;

  public int PropA1 {get {return oA.Prop1;} set {oA.Prop1 = value;}}
  public int PropA2 {get {return oA.Prop2;} set {oA.Prop1 = value;}}
  public int[] propsA3;
}

public ObjA
{
   public int Prop1 {get, set};
   public int Prop2 {get, set};

   public int getVal3(ObjB b) {return calSomethin(b);}
   public int setVal3(ref ObjB b, int val) { /*do something to ObjB*/}
}

public ObjB
{
   Byte[] data;
}

What I want for PropsA3 have a get/set behavior of the following:

Entry e;

Get: int a = e.propsA3[i]; => a = oA.getVal3(listB[i]);

Set: e.propsA3[i] = 5; => oA.setVal3(listB[i], val);

Is this possible? How can achieve this or how do I have to change the class design to get the desired result?

kiki
  • 325
  • 6
  • 20

2 Answers2

1

It is possible, but I'm not sure what the benefit would be, you can do it using a wrapper class.. eg:

public class Entry
{
    private ObjA oA;
    private ObjB[] listB;

    public int PropA1 {get {return {oA.Prop1}} set {oA.Prop1 = value;}}
    public int PropA2 {get {return {oA.Prop2}} set {oA.Prop1 = value;}}
    public EntryProperties propsA3;

    public Entry() 
    {
        propsA3 = new EntryProperties(this);
    }

    public class EntryProperties 
    {
        private Entry _entry;

        public EntryProperties(Entry entry) {
            _entry = entry;
        }

        public int this[int index] {
            get { return _entry.oA.getVal3(_entry.listB[index]); }
            set { _entry.oA.setVal3(_entry.listB[index], value); }
        }
    }
}

Having said that, I really don't think it's a good idea - why not define a view model class with the properties you want in the grid and then either set those manually, or use something like AutoMapper or ValueInjector to do so...

Martin Ernst
  • 5,629
  • 2
  • 17
  • 14
  • Thanks for the answer. I'm not set on having to access `e.propsA3[i]` but I thought my view model will have a `List – kiki Jun 26 '13 at 13:05
  • If you know what the properties will be, then I'd suggest adding them all explicitly, or if that's not really what you want, maybe look at using a DataTable or other Dictionary equivalent – Martin Ernst Jun 26 '13 at 13:15
1

This should work

pulbic Entry 
{
    public ObjA propsA3 { get; set; }
}

public ObjA
{
   public int Prop1 {get, set};
   public int Prop2 {get, set};

   public int this[ObjB b]
   {
      get { return getVal(b); }
      set { /* do something*/ }
   }

   private int getVal3(ObjB b) {return calSomethin(b);}
   private int setVal3(ref ObjB b, int val) { /*do something to ObjB*/}
}
Ation
  • 731
  • 7
  • 16