Why are you getting this error
Do you have something similar in the contents of the Resources property?
<local:BoolToCursorConverter x:Key="CursorConverter" />
If not, then what’s wrong, but I assume that you have already done this.
In this case, you suspect that you placed it in the Resources property in the Grid to which it refers. That is why it cannot be found. StaticResource resolves immediately as Xaml parses. Therefore, any key used must already be loaded into the resource dictionary before use. The Xaml parser knows nothing about the contents of the Grid Resources property, as it has not yet processed it. Consequently: -
<UserControl> <Grid Cursor="{Binding IsLoading, Converter={StaticResource CursorConverter}}"> <Grid.Resources> <local:BoolToCursorConverter x:Key="CursorConverter" /> </Grid.Resources> </Grid> </UserControl>
will fail. Where how: -
<UserControl> <UserControl.Resources> <local:BoolToCursorConverter x:Key="CursorConverter" /> </UserControl.Resources > <Grid Cursor="{Binding IsLoading, Converter={StaticResource CursorConverter}}"> </Grid> </UserControl>
at least she won’t be able to find the converter.
What do you really need to do
I presented above to answer your question, but I see that it really will not help you. You cannot bind to a Cursor property like this. (It does not open the public identifier field; Xaml uses the NameOfThing + Property convention to find the field that is the DependencyProperty for the associated property).
The solution is to create an Attached property: -
public class BoolCursorBinder { public static bool GetBindTarget(DependencyObject obj) { return (bool)obj.GetValue(BindTargetProperty); } public static void SetBindTarget(DependencyObject obj, bool value) { obj.SetValue(BindTargetProperty, value); } public static readonly DependencyProperty BindTargetProperty = DependencyProperty.RegisterAttached("BindTarget", typeof(bool), typeof(BoolCursorBinder), new PropertyMetadata(false, OnBindTargetChanged)); private static void OnBindTargetChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { FrameworkElement element = sender as FrameworkElement; if (element != null) { element.Cursor = (bool)e.NewValue ? Cursors.Wait : Cursors.Arrow; } } }
Now you can do the binding as follows: -
<Grid local:BoolCursorBinder.BindTarget="{Binding IsLoading}">