【问题标题】:Caliburn Micro and Dependency Properties binding not workingCaliburn Micro 和依赖属性绑定不起作用
【发布时间】:2013-03-06 12:53:18
【问题描述】:

我正在创建一个在其页面中包含用户控件的 WinRt 应用程序,并且我正在将 MVVM 与 Caliburn Micro 一起使用。在用户控件中,我有一个依赖属性,我将其绑定到我的视图模型中的集合,但绑定不起作用,至少在我更改模拟器的 rezolution 之前不会。我进入调试模式,用户控件的数据上下文为空,但是当我更改 rezolution 并在“SizeChanged”事件中命中断点时,我可以看到我的用户控件已正确绑定。现在我不知道是什么导致了这种延迟,因为它应该在页面加载的那一刻被绑定,但事实并非如此。 代码是这样的:

MyPage.xaml

<MyControl Users="{Binding MyUsersCollection, Mode=TwoWay}"></MyControl>

MyControl.xaml.cs

public ObservableCollection<User> Users
    {
        get { return (ObservableCollection<User>)GetValue(UsersProperty); }
        set
        {
            SetValue(UsersProperty, value);
            LoadInfo();
        }
    }

    public static readonly DependencyProperty UsersProperty =
        DependencyProperty.Register("Users", typeof(ObservableCollection<User>), typeof(MojoMap), new PropertyMetadata(new ObservableCollection<User>()));

你能帮我弄清楚这里有什么问题吗?谢谢!

【问题讨论】:

    标签: windows-8 user-controls windows-runtime dependency-properties caliburn.micro


    【解决方案1】:

    你可以试试这个:

    public ObservableCollection<User> Users
    {
        get { return (ObservableCollection<User>)GetValue(UsersProperty); }
        set
        {
            SetValue(UsersProperty, value);
        }
     }
     public static readonly DependencyProperty UsersProperty =
        DependencyProperty.Register("Users", typeof(ObservableCollection<User>), typeof(MojoMap), new PropertyMetadata(null, UsersChanged));
    
    private static void UsersChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        ((MojoMap) dependencyObject).LoadInfo();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      • 1970-01-01
      • 2012-10-12
      • 2019-12-14
      相关资源
      最近更新 更多