user3715935
user3715935

Reputation: 85

how to call method defined in view from view model

i have one function for controlling visibility of circular progressbar in view.

public void ShowProgressBarControl()
{
   CircleProgressBar.Visibility = Visibility.Visible;
   CircleProgressBar.Opacity = 1;
}

I want to call that method from view model. how can i call?

Upvotes: 1

Views: 848

Answers (1)

Eldho
Eldho

Reputation: 8301

As @tabby suggested i would use public property in viewmodel and bind to CircleProgressBar using visibility converter. For binding opactiy you can directly bind it.

Viewmodel . Assuming you have used INotifyPropertyChanged Interface. Wrap the property in INPC.

public bool IsBusy;
public int Opacity;


SomeAction()
{
   IsBusy = true;
   Thread.Sleep(5000);
   IsBusy = false;
   Opactiy = 1;

}

Converter

 public BooleanVisibiltyConverter: IValueConverter
 { 
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    bool flag = false;
    if (value is bool)
    {
        flag = (bool)value;
    }
    else if (value is bool?)
    {
        bool? nullable = (bool?)value;
        flag = nullable.HasValue ? nullable.Value : false;
    }
    return (flag ? Visibility.Visible : Visibility.Collapsed);
  }
}

XAML

Add the converter to the window resource before consuming it.

<CircularProgressBar Opacity="{Binding Opactiy}"  Visibility="{Binding IsBusy, Converter={StaticResource BoolToVis}}"

Upvotes: 1

Related Questions