简体   繁体   中英

How to bind to get-only property of API class instace?

May be my formulating of this question is incorrect ("How to bind to get-only property of API class instace?") but here's my problem: I'm creating powerpoint vsto add-in and I need to bind the SlideIndex property of concrete slide to textbox on the windows form. SlideIndex property has only get accessor. I found that in case of binding I need to use mvvm. According to examples on mvvm theme I installed MvvmLightLibs to my solution from NuGet Packege Manager and desided to "wrap" slide object with this way:

public class SlideWraper: ViewModelBase
{
   private PowerPoint.Slide Sld;
   public int SlideIndex
   {
      get
      {
         return Sld.SlideIndex;
      }
      set
      {
         RaisePropertyChanged(() => Sld.SlideIndex);
      }  
   }

   public SlideWraper(PowerPoint.Slide sld)
   {
      Sld=sld;
   }
}

Here's my code of binding creation:

...
PowerPoint.Slide ConcreteSlide=this.Application.ActivePresentation.
  Slides.FindBySlideID(257);
SlideWraper MyWraper=new SlideWraper(ConcreteSlide);
MyTextBox.DataBindings.Add(new Binding("Text", MyWraper, "SlideIndex"));
...

But this realization fills textbox with correct slide index only at the start of the program. When I replace slide (slide index changed) MyTextBox.Text is not changed.

How can I bind to get-only property of slide?

A few options here. If PowerPoint.Slide supports INPC then you should expose it directly...

private PowerPoint.Slide Sld;
public PowerPoint.Slide Slide
{
   get {return Sld;}
   set {this.Sld = value; RaisePropertyChanged(() => Slide);}  
}

... and then in XAML bind to Slide.SlideIndex .

If PowerPoint.Slide doesn't support INPC then you'll need to create a regular int property in your view model and arrange for PowerPoint.Slide to update that property in response to its SlideIndex value changing.

Finally if you know that SlideIndex has just updated then you can just call `RaisePropertyChanged("Slide")', this can be slow because it will cause all bindings to the Slide properties to update, but sometimes it's the only choice.

Either way, automatic updates can't just magically work by themselves, PowerPoint.Slide needs to have some mechanism to notify the rest of the program that its SlideIndex property has changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM