I am new to WPF MVVM. Here's what my viewmodel is doing:
A button is pressed and a ping command is launched to see if servers are availables:
-If true, the button is set to Hidden.
-If false, a label with a message ("Servers not availables) is set to visible
How can I reuse the following IsVisible method to set the Label's visibility?
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
namespace WpfTest
{
public class PrnViewModel1 : ViewModelBase
{
private ICommand m_PrnServPingCommand;
private Visibility _isVisible=Visibility.Visible;
public PrnViewModel1()
{
PrnServPingCommand = new RelayCommand(new Action<object>(PrnServPing));
}
public ICommand PrnServPingCommand
{
get
{
return m_PrnServPingCommand;
}
set
{
m_PrnServPingCommand = value;
}
}
public void PrnServPing(object obj)
{
string[] serverNames = { "svmsimp1", "svmsimp2" };
bool serversArePingable = Cmethods.PingableAll(serverNames);
if (serversArePingable)
{
IsVisible = Visibility.Hidden; //Button is Hidden
}
else
{
//*** Label with Message "Servers not pingable" set to visible
}
}
public Visibility IsVisible
{
get
{
return _isVisible;
}
set
{
_isVisible = value;
OnPropertyChanged("IsVisible");
}
}
}
}
You can use an
IValueConverter
to reverse theVisibility
on your label.Here is an example implementation:
Usage:
Don't forget to instantiate the converter in the
Resources
. See here for a tutorial on converters.