Wednesday, 19 August 2009

C# Inline If Sample

Sample using WPF

Button newbutton = new Button();
bool a = true;
newbutton.Visibility = a ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;

(a) being a bool or it could have been String.IsNullEmpty(newbutton.Text)

newbutton.Visibility = !String.IsNullEmpty(newbutton.Text) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;

Anything that evaluates to bool. If True do the command after the ? else do the command after the :

0 comments: