2012-07-23 11 views
0

當自定義UserControl的目標DependencyProperty更改時,我得到的綁定源不會更新。在自定義用戶控件中使用DependencyProperty更新源代碼

源代碼是一個加載了MEF的ViewModel到我的自定義UserControl的DataContext中。

我的結合看起來像這樣的的MyUserControl

// MyUserControl.xaml 
<MyUserControlBase x:Class="MyUserControl" MyDependencyProperty="{Binding ViewModelProperty}"> 

如果我使用MyDependencyProperty與FrameworkPropertyMetadata和使用回調函數,當屬性的變化,我可以做哪些工作正常,以下根XAML

// MyUserControlBase.cs 
private static void MyDependencyProperty_Changed(DependencyObject depObj, DependencyPropertyChangedEventArgs args) 
{ 
    var filter = (UserControl)depObj; 
    var vm = (ViewModel)filter.DataContext; 
    vm.ViewModelProperty= (VMPropType)args.NewValue; 
} 

在繼承UserControl的MyUserControlBase中註冊DependencyProperty。

// MyUserControlBase.cs 
public static readonly DependencyProperty MyDependencyPropertyProperty = DependencyProperty.Register("MyDependencyProperty", 
      typeof(VMPropType), 
      typeof(MyUserControlBase), 
      new FrameworkPropertyMetadata(null, MyDependencyProperty_Changed)); 

但是爲什麼我不能在XAML中完成簡單的雙向綁定工作?

輸出窗口顯示沒有綁定失敗。我可以看到正在調用ViewModel.ViewModelProperty getter。一旦MyDependencyProperty發生變化,就不要使用setter。

它似乎不是事件順序的問題。 ViewModel在DependencyProperty第一次被改變之前就已經創建好了。

我也看過Setting up binding to a custom DependencyProperty inside a WPF user control 但他的問題似乎稍有不同,因爲我actualy從我自己的基類持有DependencyProperty繼承。

謝謝你的幫助。

編輯:鏈接到示例解決方案。 Solution zip here

對不起,可怕的鏈接,我不知道很多快速fileshareing網站。如果它的壞評論,我會盡快刪除它,但它似乎確定。

+0

請在註冊依賴項屬性的位置填寫代碼。 – Jefim 2012-07-23 12:48:40

回答

0

回答更新/不相干的信息刪除

看你的樣品中的代碼,我看到的問題是什麼之後。你有這個「選擇器」控制。在BindDependencies方法中,您在MyControlBase.MyDependencyProperty上設置綁定。但同時你也將這個屬性綁定到你的控件的datacontext(在MyUserControl.xaml)。實際上,這意味着在BindDependencies方法中,您將覆蓋綁定到您的viewmodel,因此在該方法之後它不是活動的 - 您可以通過分入SetBinding呼叫之前和之後來訪問並調用BindingOperations.GetBindingExpression(control, MyUserControlBase.MyPropertyDependencyProperty) - 綁定是不同的。這就是爲什麼你的代碼不起作用。我想你必須找到轉移的價值有:)

一些額外的信息

在WPF屬性只能有一個綁定的目標的另一種方式。這實際上非常合乎邏輯 - 如果一個屬性綁定到兩個源 - 它應該使用哪一個?綁定被標記爲OneWayToSource的事實在這種情況下並不重要,因爲它仍然作爲綁定來對待控件的屬性。

您可以調查並查看它是否適用於您的一種方法是MultiBinding - 它允許通過實施您自己的IMultiValueConverter將控件的屬性綁定到多個源。您可以在所提供的鏈接中的MSDN上找到關於此的更多信息。

0

你暴露ViewModelProperty作爲一個普通的屬性..

通常你做以下....

public static readonly DependencyProperty ViewModelPropertyProperty = DependencyProperty.Register("ViewModelProperty", 
      typeof(VMPropType), 
      typeof(MyUserControlBase), 
      new FrameworkPropertyMetadata(null, ViewModelProperty_Changed)); 

public VMPropType ViewModelProperty { 
    get { return (VMPropType)GetValue(ViewModelPropertyProperty); } 
    set { SetValue(ViewModelPropertyProperty,value); } 
} 

編輯 - 註釋之後。

你什麼時候檢查房屋改變了?默認情況下,WPF綁定僅在失去焦點時更新值,您更改更改綁定的UpdateSourceTrigger屬性以改變此行爲。

+0

在MyUserControlBase_Changed方法中,它是DependencyProperty的Register方法的回調。 DependencyPropertyChangedEventArgs.NewValue顯示正確。它只是不更新​​我的XAML綁定源。我已經嘗試使用UpdateSourceTrigger = PropertyChanged。 – CodeMonkey 2012-07-23 14:39:31

+0

您可以在ViewModelProperty的實際設置器中添加一個檢查。您可能檢查得太早,而且這種變化還沒有經過級聯。 – 2012-07-23 14:42:36

+0

我已經做到了,但只有當我做'新的MyUserControl()「和MEF創建ViewModel時纔會被命中。我得到了兩個get的返回null和一個賦值爲null的集合。之後,我看到一個在MyUserControlBase上的過程.MyDependencyProperty_Changed其中args包含正確的值,但ViewModel中沒有任何更改。 – CodeMonkey 2012-07-23 15:25:43

相關問題