C# 在Silverlight 3.0中,如何将网格的Cursor属性绑定到my ViewModel的属性?

C# 在Silverlight 3.0中,如何将网格的Cursor属性绑定到my ViewModel的属性?,c#,silverlight,xaml,silverlight-3.0,C#,Silverlight,Xaml,Silverlight 3.0,我正在尝试将IsLoading属性绑定到UI的LayoutRoot网格的Cursor属性。我试图让主应用程序光标在属性显示正在加载时变成沙漏 我将按如下方式绑定该属性: <Grid Cursor="{Binding IsLoading, Converter={StaticResource CursorConverter}}"> 当我尝试运行这个时,虽然我得到了XamlParseException“给定的键在字典中不存在。” 如果您有任何帮助,我们将不胜感激。您为什么会出现此错误 在

我正在尝试将IsLoading属性绑定到UI的LayoutRoot网格的Cursor属性。我试图让主应用程序光标在属性显示正在加载时变成沙漏

我将按如下方式绑定该属性:

<Grid Cursor="{Binding IsLoading, Converter={StaticResource CursorConverter}}">
当我尝试运行这个时,虽然我得到了XamlParseException“给定的键在字典中不存在。”


如果您有任何帮助,我们将不胜感激。

您为什么会出现此错误

在资源属性的内容中是否有类似的内容

<local:BoolToCursorConverter x:Key="CursorConverter" />
将失败。其中:-

<UserControl>
   <UserControl.Resources>
     <local:BoolToCursorConverter x:Key="CursorConverter" />
   </UserControl.Resources >
   <Grid Cursor="{Binding IsLoading, Converter={StaticResource CursorConverter}}">
     <!-- Contents here -->
   </Grid>
</UserControl>
现在您可以像这样进行绑定:-

 <Grid local:BoolCursorBinder.BindTarget="{Binding IsLoading}">

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