I need to change the Window’s cursor to a hourglass depending on a boolean property in the ViewModel, to achieve this, I have defined a Converter which converts bool to Cursor like below:
[ValueConversion(typeof(bool), typeof(Cursors))]
public class CursorExtensionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && ((bool)value))
{
return Cursors.Wait;
}
return Cursors.Arrow;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
In XAML, I have the following markup to bind the Window.Cursor to the converter
<Window.Resources>
<Converters:CursorExtensionConverter x:Key="cursorExtensionConverter"/>
</Window.Resources>
<Window.Cursor>
<Binding Path="IsBusy" Converter="{StaticResource cursorExtensionConverter}"/>
</Window.Cursor>
In the ViewModel, When I set IsBusy = true; the Convert function in the CursorExtensionConverter is not being called. Why?
Thanks
The problem is due to the order of when the window’s DataContext is set to ViewModel and when the XAML is parsed. Also it is related to how the PropertyChanged event is handled in our implementation of ViewModel. Which I am not be able to paste here.
I haven’t fully figure it out but in this particular case building the binding in code behind solves the problem: