2017-02-22 208 views
2

我創建了我的用戶有Owner財產依賴屬性空

<shared:NavigationControl Owner="{Binding ElementName=This, Converter={StaticResource TestConverter}}" /> 

This財產是我的網頁x:Name

這裏是後面的代碼:

public MvxWindowsPage Owner 
{ 
    get { return (MvxWindowsPage)GetValue(OwnerProperty); } 
    set { SetValue(OwnerProperty, value); } 
} 

public static readonly DependencyProperty OwnerProperty = 
       DependencyProperty.Register("Owner", typeof(MvxWindowsPage), typeof(NavigationControl), null); 

我創建TestConverter檢查結合是確定的,它是。

爲什麼Owner屬性是null in cs?

+0

您測試過這不是空嗎?顯示你在哪裏/如何設置這個。 – loopedcode

+0

你可以顯示轉換器,你在那裏返回?我懷疑問題在轉換器內部 - 你有沒有'返回值';最後? – Romasz

+0

當然,我認爲問題是,在窗口被加載後,價值獲得綁定,我想要在我的結構和它的綁定可能會得到的價值? – miechooy

回答

0

問題很可能在您的This屬性中。你將不得不確認它不是空的。添加一個PropertyChanged回調,然後調試並確認發送到您的OwnerProperty的值不爲null。

public MvxWindowsPage Owner 
{ 
    get { return (MvxWindowsPage)GetValue(OwnerProperty); } 
    set { SetValue(OwnerProperty, value); } 
} 

public static readonly DependencyProperty OwnerProperty = 
       DependencyProperty.Register("Owner", typeof(MvxWindowsPage), typeof(NavigationControl), new PropertyMetadata(PropChanged)); 

public static void PropChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
{ 
     var value = e.NewValue; //confirm this isn't null 
} 
+0

e.NewValue包含適當的值; O – miechooy

0

正如我已檢查的問題是,綁定可先在你的頁面加載事件哪裏是的ElementName(你是不是設置的DataContext任何地方)。如果您希望該值在構造函數中可用,那麼您將不得不先調用綁定更新。這可以通過設置的DataContext這將調用過程來完成,但是存在與x:Bind一個簡單的方法:在頁面的構造函數,你可以ivoke這樣的綁定更新

<local:MyUserControl x:Name="myUsrCtrl" Owner="{x:Bind This, Mode=OneWay, Converter={StaticResource MyConverter}}"/> 

則:

public BlankPage() 
{ 
    this.InitializeComponent(); 
    this.Bindings.Update(); 
    var aaa = myUsrCtrl.Owner; // this shouldn't be null now 
    // rest of code 

雖然也許改變你的邏輯會比在構造函數中調用更新更好。

+0

非常感謝Yor時間。關鍵是我有這個用戶控件是導航欄。我想讓它在我的應用程序的任何頁面上可見。導航行爲是突出顯示當前查看的頁面。我以爲我可以有這個導航欄當前頁面,所以我可以突出顯示此導航欄控件中的圖標 – miechooy

+2

@miechooy然後,我會去爲不同的設計。使你的窗戶內容成爲一個*導航欄*,導航欄在底部,其餘部分填充框架,這將保留其餘的頁面。然後在你的導航欄中,你可以綁定到'Frame.Content'(如果需要,可以進行一些轉換)。 – Romasz