1

in my model i have the following property

private string somestring;
public string SomeString
        {
            get
            {
                return somestring;
            }
            set
            {
                SetField(ref somestring, value, "SomeString");
            }
        }

and in my view model i have an ObservableCollection which represents selected objects

private ObservableCollection<Models.model> selectedObjects
public ObservableCollection<Models.model> SelectedObjects
        {
            get
            {
                return selectedObjects;
            }
            set
            {
                SetField(ref selectedObjects, value, "SelectedObjects");
            }
        }

suppose i want to set the same value of property for every object in the collection whenever that property value changes in the currentObject so i came up with this in my viewmodel

 private void SetMultiplePropertyValues<T>(string propName, object value)
        {
            if(!SelectedObjects.Contains(currentObject)) return;
            var p = typeof(T).GetProperty(propName);
            foreach (var obj in SelectedObjects)
            {
                p.SetValue(obj, value, null);
            }

        }

is there a more convenient way than calling that function when ever a property changes

FPGA
  • 3,525
  • 10
  • 44
  • 73
  • Put the property on the collection? – Tony Hopkinson Nov 13 '13 at 12:49
  • @TonyHopkinson could you please explain more? – FPGA Nov 13 '13 at 12:52
  • It looks like you are doing a lot of work with reflection - can you explain why? Reflection isn't very performant - can you not just set the property value on each item in the list or do you need to create a generic helper method? – Charleh Nov 13 '13 at 12:53
  • @Charleh problem is i have alot of properties with different types, so i want it to be dynamic rather than having to write the function for each property – FPGA Nov 13 '13 at 12:56
  • 2
    Can you explain the use case? I can't say I'd ever feel the need to write a function to set property values in a collection when you can do it in one/two lines of code - e.g. Linq - `List.ForEach(x=>x.Property = "someValue");` and use `ToList()` if you don't have a list. Alternatively `foreach(var item in Enumerable) item.Property = "someValue";` - still 1/2 lines. – Charleh Nov 13 '13 at 12:59
  • 1
    @Charleh Jeff's tip: if you need to call the method multiple times, use reflection once to find it, then assign it to a delegate, and then call the delegate. form here http://stackoverflow.com/questions/25458/how-costly-is-net-reflection – FPGA Nov 13 '13 at 13:01
  • @Charleh i think its better to write it for each property to avoid reflection as you suggested – FPGA Nov 13 '13 at 13:07
  • please explain the use case – makc Nov 13 '13 at 13:11
  • @makc i display those objects in a datagrid, wen multiple objects are selected, for some properties i want the value to be the same, for example you use it to lable some objects that belong to the same category without having to set it for each object alone, i use it for google data feed adwords tag values – FPGA Nov 13 '13 at 13:16
  • I think the OP means he has different object types though some of them may contain a property with same name... – dev hedgehog Nov 13 '13 at 13:18
  • @devhedgehog i want to use the function for different types, the function is called on property changed, i use INotifyProperyChanged, property name doesnt matter since i send it as a parameter – FPGA Nov 13 '13 at 13:24
  • 1
    Create Interface called IObjectWithOnPropertyChangedMethod, place a method inside called OnPropertyChanged. Let your SelectedItems implement that Interface. Later when needed just cast to that Interface. :) – dev hedgehog Nov 13 '13 at 13:27
  • @devhedgehog honestly i've never used interfaces that i wrote on my own but will try them this time – FPGA Nov 13 '13 at 13:36
  • Let me post you code in an answer. – dev hedgehog Nov 13 '13 at 13:37

1 Answers1

2

Here is an example:

interface IObjectWithOnPropertyChangedMethod
{
  void OnPropertyChanged(string propertyName);
}

public class MyPoco : INotifyPropertyChanged, IObjectWithOnPropertyChangedMethod
{
  //// Implementation of IObjectWithOnPropertyChangedMethod interface
  public void OnPropertyChanged(string propertyName)
  {
    if(PropertyChanged != null)
    {
       PropertyChanged(.....);
    }
  }

 //// Implementation of INotifyPropertyChanged interface
 public event PropertyChanged;
}

Something like that and now when you want to fire OnPropertyChanged though your list of objects just do something like this:

foreach (var obj in list)
{
  if(obj is IObjectWithOnPropertyChangedMethod)
  {
    ((IObjectWithOnPropertyChangedMethod)obj).OnPropertyChanged("MyProperty");
  }
}

I hope this helps.

dev hedgehog
  • 8,698
  • 3
  • 28
  • 55