【问题标题】:How to Bind the Cursor property of a Grid to a Property of my ViewModel in Silverlight 3.0?如何在 Silverlight 3.0 中将 Grid 的 Cursor 属性绑定到我的 ViewModel 的属性?
【发布时间】:2010-02-04 21:09:50
【问题描述】:

我正在尝试将 IsLoading 属性绑定到我的 UI 的 LayoutRoot Grid 的 Cursor 属性。每当属性说正在加载时,我都试图让主应用程序光标变成沙漏。

我将属性绑定如下:

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

“CursorConverter”键映射到资源中的 BoolToCursorConverter。转换器代码为:

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;
    }
}

当我尝试运行它时,虽然我得到 XamlParseException "The given key was not present in the dictionary."

任何帮助将不胜感激,谢谢,

【问题讨论】:

    标签: c# silverlight xaml silverlight-3.0


    【解决方案1】:

    为什么会出现该错误

    Resources 属性的内容中是否有类似的内容?

    <local:BoolToCursorConverter x:Key="CursorConverter" />
    

    如果不是,那就是问题所在,但我猜你已经这样做了。

    在这种情况下,我怀疑您已将其放置在它适用的 GridResources 属性中。这就是找不到它的原因。 StaticResource 在解析 Xaml 时立即解析。因此,使用的任何键必须在使用之前已经加载到资源字典中。 Xaml 解析器对 Grid 的 Resources 属性的内容一无所知,因为它尚未处理它。因此:-

    <UserControl>
       <Grid Cursor="{Binding IsLoading, Converter={StaticResource CursorConverter}}">
         <Grid.Resources>
           <local:BoolToCursorConverter x:Key="CursorConverter" />
         </Grid.Resources>
         <!-- Contents here -->
       </Grid>
    </UserControl>
    

    会失败。其中:-

    <UserControl>
       <UserControl.Resources>
         <local:BoolToCursorConverter x:Key="CursorConverter" />
       </UserControl.Resources >
       <Grid Cursor="{Binding IsLoading, Converter={StaticResource CursorConverter}}">
         <!-- Contents here -->
       </Grid>
    </UserControl>
    

    至少不会找不到转换器。

    你真正需要做的事情

    我已经介绍了以上内容来回答您的问题,但我可以看到它并没有真正帮助您。您不能像这样绑定到 Cursor 属性。 (它不公开公共标识符字段,Xaml 使用 NameOfThing + "Property" 约定来查找被绑定属性的 DependencyProperty 字段。

    解决方案是创建一个附加属性:-

    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}">
    

    【讨论】:

      猜你喜欢
      • 2011-01-19
      • 1970-01-01
      • 2015-01-25
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 2011-08-01
      • 1970-01-01
      相关资源
      最近更新 更多