MVVM Binding To Property == Null?

Rather than using a BooleanToVisibilityConverter you'll need to use a different converter (one you'll have to write) that will return the appropriate visibility value if the bound value is null.

Up vote 0 down vote favorite share g+ share fb share tw.

I want to show some elements when a property is not null. What is the best way of achieving this? The following is my ViewModel: class ViewModel : ViewModelBase { public Trade Trade { get { return _trade; } set { SetField(ref _trade, value, () => Trade); } } private Trade _trade; } ViewModelBase inherits INotifyPropertyChanged and contains SetField() The Following is the Trade class: public class Trade : INotifyPropertyChaged { public virtual Company Company { get { return _company; } set { SetField(ref _company, value, () => Company); } } private Company _company; ...... } This is part of my View.

Xaml I would like this groupbox to show only if Trade. Company is not null (so when a user selects a company). Would I need to create a custom converter to check for null and return the correct visibility or is there one in .

NET? C# wpf data-binding mvvm link|improve this question asked Jun 9 '10 at 17:06LnDCobra1,8851031 98% accept rate.

Rather than using a BooleanToVisibilityConverter, you'll need to use a different converter (one you'll have to write) that will return the appropriate visibility value if the bound value is null. Something like this: public class NullValueToVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (value! = null?

Visibility. Visible : Visibility. Collapsed); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; // this shouldn't ever happen, since // you'll need to ensure one-way binding } } You'll need to add Mode = OneWay to your binding, since you won't be able to make a round-trip conversion.

Thanks came up with that idea when I got to the bit where I pasted the xaml code, and realised I could make a custom converter, thanks for the code greatly appreciated! – LnDCobra Jun 9 '10 at 19:49.

You can also use DataTriggers to do pretty much the same thing without the converter...

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions