我在Silverlight中有一个数据导航用户控件,它打开一个子窗口,用户可以在其中输入搜索条件,当他们按下“应用”时,它会更新ViewModel中的绑定属性(MVVM模式。)
链接是: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");
}
}
}
我一直在努力寻找问题,但没有取得任何成功;谁有人看到这个问题?提前谢谢,
答案 0 :(得分:1)
由于您尝试将其绑定到string类型的属性,因此不确定中间的Binding属性是否有效。
由于你已经使用了一些Nikhil的东西,你可能想看看how he uses Tasks to handle dialogs/child windows,同时仍然保持MVVM范例。
答案 1 :(得分:0)
Bryant's solution看起来是正确的方法,但我仍然遇到了一些问题。我最终使用以下方法让它工作:
我向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,以便对搜索属性的更改将传播到DataNavigator,然后绑定到V&amp; 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;
}
剩下的事情和上面的情况一样,我正在寻找我正在寻找的效果。