1

我創建了一個WinRt應用程序,該應用程序在其頁面中具有用戶控件,而且我正在使用MVVM和Caliburn Micro。在用戶控件中,我有一個依賴項屬性,我綁定到我的視圖模型中的一個集合,但綁定不起作用,至少直到我更改模擬器的重新解析爲止。我進入調試模式,並且用戶控件的數據上下文爲空,但是當我更改rezolution並在「SizeChanged」事件中打斷點時,我可以看到我的用戶控件已正確綁定。現在我不知道是什麼造成了這種延遲,因爲它應該在頁面加載的時候綁定,但事實並非如此。 的代碼是這樣的:Caliburn Micro和依賴項屬性綁定不起作用

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>())); 

你能不能幫我找出這裏有什麼問題嗎?謝謝!

回答

0

你能試試這個:

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