I have a dependency property of type collection, when its callback fires based on the count I need to set the visibility of some of the controls on the screen.
But t
You have made the classic mistake of binding to auto properties that are valid for binding, but don't notify upon change, which means the binding subsystem cannot detect changes and update the binding targets.
To fix this, implement INotifyPropertyChanged on your viewmodel, and then ensure that you notify the property change from the properties.
As an example, I have the following in the base class for my viewmodels:
public abstract class BaseViewModel : INotifyPropertyChanged
{
///
/// Helper method to set the value of a property and notify if the value has changed.
///
///
/// The value to set the property to.
/// The current value of the property.
/// Flag indicating whether there should be notification if the value has changed.
/// The property names to notify that have been changed.
protected bool SetProperty(ref T newValue, ref T currentValue, bool notify, params string[] notifications)
{
if (EqualityComparer.Default.Equals(newValue, currentValue))
return false;
currentValue = newValue;
if (notify && notifications.Length > 0)
foreach (string propertyName in notifications)
OnPropertyChanged(propertyName);
return true;
}
///
/// Raises the event.
///
/// The name of the property that changed.
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
///
/// Occurs when a property value changes.
///
public event PropertyChangedEventHandler PropertyChanged;
}
then in your regular viewmodel:
public class MyViewModel : BaseViewModel
{
private bool _countLabelVisible;
public bool CountLabelVisible
{
get { return _countLabelVisible; }
set { SetProperty(ref value, ref _countLabelVisible, true, "CountLabelVisible", "CountLabelVisibleReverse"); }
}
public bool CountLabelVisibleReverse { get { return !_countLabelVisible; }}
}
This way, when CountLabelVisible
gets changed it also notifies on the property CountLabelVisibleReverse
, and the property CountLabelVisibleReverse
consists of only a getter - because it will always be the inverse of CountLabelVisible
.
So that fixes your code the way you have it, but the reality is you don't need to keep the CountLabelVisibleReverse
property, instead you could: