How do I link the Cursor property of a grid to an object of my ViewModel in Silverlight 3.0?

I am trying to bind the IsLoading property to the Cursor property of my LayoutRoot grid. I am trying to get the main pointer of the application to become sandy sand whenever the property says that it is loading.

I bind the property as follows:

<Grid Cursor="{Binding IsLoading, Converter={StaticResource CursorConverter}}"> 

The "CursorConverter" key is displayed in the BoolToCursorConverter in the resources. Converter Code:

 public class BoolToCursorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (parameter == null) return ((bool)value == true) ? Cursors.Wait : Cursors.Arrow; return false; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { Cursor cursor = value as Cursor; if (cursor != null) return cursor == Cursors.Wait ? true : false; return false; } } 

When I try to run this, although I get the XamlParseException "The specified key was not present in the dictionary."

Any help would be appreciated, thanks,

+4
source share
1 answer

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> <!-- Contents here --> </Grid> </UserControl> 

will fail. Where how: -

 <UserControl> <UserControl.Resources> <local:BoolToCursorConverter x:Key="CursorConverter" /> </UserControl.Resources > <Grid Cursor="{Binding IsLoading, Converter={StaticResource CursorConverter}}"> <!-- Contents here --> </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}"> 
+6
source

Source: https://habr.com/ru/post/1300338/


All Articles