想要在wpf中显示/隐藏ViewModel中的控件

时间:2014-01-30 06:09:48

标签: c# wpf mvvm

我想在wpf中显示/隐藏ViewModel中的控件。但NotifyingProperty在这里工作不正常是我的代码。

这是我对设计视图的控制

<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); }
    }

在ViewModel的构造函数上,我设置了

public AdvanceSearchViewModel()
    {
       IsProcessStart = false;
    }

我正在按钮点击命令更改此属性,但它无法正常工作。 Plz的帮助。 如果这将在Dependency属性中起作用,那么我可以在ViewModel中使用Dependency属性。

Ater添加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));
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您应该在ViewModel

中实现这样的INotifyPropertyChanged
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"); }
           }
}

要设置此属性,只需按照ViewModel进行操作并将其设置为 -

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

答案 1 :(得分:0)

您不应在ViewModel中使用DependencyProperty。

在ViewModel中应该做的是,实现INotifyPropertyChanged

MSDN - How to: Implement Property Change Notification