簡體   English   中英

想要在wpf中顯示/隱藏ViewModel中的控件

[英]Want to show/hide control from ViewModel in wpf

我想在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));
        }
    }
}

您應該在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;
  }

您不應在ViewModel中使用DependencyProperty。

您應該在ViewModel中執行的操作是,實現INotifyPropertyChanged

MSDN - 如何:實現屬性更改通知

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM