简体   繁体   中英

Want to show/hide control from ViewModel in wpf

I Want to show/hide control from ViewModel in wpf. But NotifyingProperty not working fine here is my code.

This is my control on design View

<ZuneWithCtrls_UserControls:SmallClock Visibility="{Binding IsProcessStart, Converter={StaticResource BooleanToVisibilityConverter}}" 
                Height="Auto" Width="Auto" VerticalAlignment="Center" HorizontalAlignment="Center" Canvas.Top="150"/>

ViewMode

/// <summary>
    /// The NotifyingProperty for the IsProcessStart property.
    /// </summary>
    private readonly NotifyingProperty IsProcessStartProperty =
      new NotifyingProperty("IsProcessStart", typeof(bool), default(bool));

    /// <summary>
    /// Gets or sets IsProcessStart.
    /// </summary>
    /// <value>The value of IsProcessStart.</value>
    public bool IsProcessStart
    {
        get { return (bool)GetValue(IsProcessStartProperty); }
        set { SetValue(IsProcessStartProperty, value); }
    }

On Constructor of ViewModel I set

public AdvanceSearchViewModel()
    {
       IsProcessStart = false;
    }

And I am changing this property on button click command but it's not working. Plz help. If this will work from Dependency property then who can i use Dependency property in ViewModel.

Ater Adding INotification

[ViewModel]
public class AdvanceSearchViewModel : PageViewModel, INotifyPropertyChanged
{
    /// <summary>
    /// Initializes a new instance of the <see cref="AdvanceSearchViewModel"/> class.
    /// </summary>
    /// 
    public event PropertyChangedEventHandler PropertyChanged;
    public AdvanceSearchViewModel()
    {

        IsProcessStart = false;

    }

    //IsProcessStart
    private bool _IsProcessStart;
    public bool IsProcessStart
    {
        get { return _IsProcessStart; }
        set
        {
            _IsProcessStart = value;
            OnPropertyChanged("IsProcessStart");
        }
    }

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

You should implement INotifyPropertyChanged like this in your ViewModel

public class AdvanceSearchViewModel: INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    .....
    Your Properties Here
    .....

    private bool _isProcessStart
    public bool IsProcessStart
    {
       get { return _isProcessStart; }
       set { 
             _isProcessStart = value; 
             OnPropertyChanged("IsProcessStart"); }
           }
}

For setting this property, just follow up to your ViewModel and set it like this -

private void MyButton_Click(object sender, RoutedEventArgs e)
  {
    var isStarted = ViewModel.IsProcessStart;
    ViewModel.IsProcessStart = !isStarted;
  }

You should not use DependencyProperty in your ViewModel.

What you should do in your ViewModel is , implement INotifyPropertyChanged

MSDN - How to: Implement Property Change Notification

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