-1

I want to add a Property to the Microsoft.Exchange.WebServices.Data.Folder class. The reason for this is, I need a bool Property "Selected" to bind it in WPF to a checkbox.

First I thought I can use C#-Extensions but currently it's not possible to write a property extension.

Then I created my own class "MyFolder" so I can cast from the Folder class to my class.

It doesn't work either.

public class MyFolder : Folder, INotifyPropertyChanged
{
    private bool selected;

    public bool Selected
    {
        get { return selected; }
        set { selected = value; OnPropertyChanged("Selected"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public MyFolder(ExchangeService service):base(service)
    {

    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

Edit:

I know it would be possible to add a property Folder to the class MyFolder. But I thought it must be possible in a elegant way.

public class MyFolder : INotifyPropertyChanged
{
    private bool selected;

    public bool Selected
    {
        get { return selected; }
        set { selected = value; OnPropertyChanged("Selected"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public Folder FolderObject { get; set; }

    public MyFolder(Folder FolderObject)
    {
        this.FolderObject = FolderObject;
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}
Joba
  • 828
  • 9
  • 28

2 Answers2

2

You don't need to add a property to an existing class. As you are in MVVM, why not simply implement a View Model that represents that functionality.

So for example:

class MyFolderViewModel : INotifyPropertyChanged
{
    public bool IsSelected { get; set; }

    private Folder folderObject;
}
S. Spindler
  • 546
  • 3
  • 12
  • Thanks! Thats a good way, but I am not using MVVM. I will remember it in future! – Joba Jun 14 '17 at 11:36
  • Generally you use MVVM. Your class `MyFolder` acts as a View Model since it implements `INotifyPropertyChanged`. What do you want to achieve with `MyFolder`? – S. Spindler Jun 14 '17 at 12:05
1

I want to add a Property to the Microsoft.Exchange.WebServices.Data.Folder class.

This is indeed not possible since you haven't defined this class yourself and thus you cannot change its definition. Adding a property to a class would require you to recompile it.

Then I created my own class "MyFolder" so I can cast from the Folder class to my class.

You cannot cast a Microsoft.Exchange.WebServices.Data.Folder object to a custom type of yours...this will never work.

What you should do is to create your own class, e.g. MyFolder, that wraps any functionality of the Microsoft.Exchange.WebServices.Data.Folder class that you need. This is basically what you have done already I guess. There is no better way.

You then bind directly to an instance of your client-aware class (MyFolder) that implements the INotifyPropertyChanged interface. This is a good and recommended approach really. Forget about adding properties to a build-in class and casting.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • And how can I convert an existing Folder object to a MyFolder object? – Joba Jun 14 '17 at 11:32
  • You can't convert it. You should wrap it in your custom class. Then you can simply bind to Folder.SomeProperty of your MyFolder object. – mm8 Jun 14 '17 at 11:38
  • @Waterfront you can't cast Folder into myFolder however you can set up an implicit or Explixt converter that will allow you to convert see https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/implicit – MikeT Jun 14 '17 at 12:15