簡體   English   中英

將Silverlight自定義控件依賴項屬性綁定到模型屬性

[英]Issue binding Silverlight Custom Control dependency property to Model property

我在Silverlight中有一個數據導航用戶控件,該控件打開一個子窗口,用戶可以在其中輸入搜索條件,並且當他們按“應用”時,假設要更新ViewModel(MVVM模式)中的bound屬性。

鏈接是:SearchDialog <-> DataNavigator <-> MyView <-> MyViewModel

當我設置它的值時,SearchDialog中的依賴項屬性似乎可以工作,它顯示在DataNavigator中。 但是,當依賴項屬性更改時,似乎沒有通知從DataNavigator發送到MyView / MyViewModel。

SearchDialog源自ChildWindow:

public string Search
{
 get { return (string)GetValue(SearchProperty); }
 set { SetValue(SearchProperty, value); }
}

public static readonly DependencyProperty SearchProperty =
 DependencyProperty.Register("Search", typeof(string), typeof(SearchDialog),
 new PropertyMetadata(null));

DataNavigator源自UserControl:

public Binding Search { get; set; }

private void DataNavigator_Loaded(object sender, Windows.RoutedEventArgs e)
{
 if (Search != null)
  this._searchDialog.SetBinding(SearchDialog.SearchProperty, Search);
}

MyView源自SilverlightFX.UserInterface.Navigation.Page:

<DataNavigator MovePreviousAction="$model.MovePrevious()"
               CurrentIndex="{Binding CurrentIndex, Mode=TwoWay}"
               MoveNextAction="$model.MoveNext()"
               SaveAction="$model.SaveChanges()"
               IsLoading="{Binding IsLoading, Converter={StaticResource VisibilityConverter}}"
               Search="{Binding SearchString, Mode=TwoWay}"/>

MyViewModel派生自ViewModel:

public string SearchString
    {
     get { return this._search; }

  set
  {
   if(value != this._search)
   {
    this._search = value;
    this.RaisePropertyChanged("SearchString");
   }
  }
 }

我一直在努力尋找問題的時間,但是沒有成功。 有人看到這個問題嗎? 提前致謝,

不確定中間的Binding屬性是否可以正常工作,因為您正在嘗試將其綁定到string類型的屬性。

由於您已經在使用Nikhil的某些東西,因此您可能想看看他如何使用Tasks處理對話框/子窗口,同時仍保持MVVM范例不變。

科比的解決方案看起來是正確的方法,但我仍然遇到一些問題。 我確實使用以下方法使它起作用:

我向SearchDialog添加了一個事件,只要搜索模式DP發生更改,該事件就會觸發:

public string Search
{
    get { return (string)GetValue(SearchProperty); }
    set { SetValue(SearchProperty, value); }
}

public static readonly DependencyProperty SearchProperty =
    DependencyProperty.Register("Search", typeof(string), typeof(SearchDialog),
    new PropertyMetadata(null, new PropertyChangedCallback(OnSearchChanged)));

private static void OnSearchChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    // call to instance method
    ((SearchDialog)d).OnSearchChanged(e);
}

protected virtual void OnSearchChanged(DependencyPropertyChangedEventArgs e)
{
    if (SearchChanged != null)
        this.SearchChanged(this, e);
}

public event DependencyPropertyChangedEventHandler SearchChanged;

之后,我在DataNavigator中添加了另一個DP:

public string Search
{
    get { return (string)GetValue(SearchProperty); }
    set { SetValue(SearchProperty, value); }
}

public static readonly Windows.DependencyProperty SearchProperty =
Windows.DependencyProperty.Register("Search", typeof(string), typeof(DataNavigator),
new Windows.PropertyMetadata(null));

然后將導航器的DP連接到SearchDialog的DP,以便將對Search屬性的更改傳播到DataNavigator,然后將其綁定到V&VM。

// Inside the Loaded Event:
this._searchDialog.SearchChanged +=
    new System.Windows.DependencyPropertyChangedEventHandler(Search_Changed);

// Handler:
private void Search_Changed(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
{
    string search = this._searchDialog.GetValue(SearchDialog.SearchProperty) as string;
    this.Search = search != null ? search.ToString() : null;
}

其余的事情與上面相同,並且我正在獲得想要的效果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM