【问题标题】:Issue binding Silverlight Custom Control dependency property to Model property将 Silverlight 自定义控件依赖项属性绑定到模型属性问题
【发布时间】:2010-12-16 00:56:09
【问题描述】:

我在 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");
   }
  }
 }

我已经尝试了几个小时来找到问题,但没有任何成功;有人看到这个问题吗?提前致谢,

【问题讨论】:

  • 我认为的 xaml 是 DataNavigator?那是视图还是视图中的 DataNavigator?我也没有看到您的 VM 中的 SearchString 属性与视图中的搜索绑定(或者是 DataNavigator?)之间有任何关系。
  • @Bryant 我只剪辑了 MyView 中的 部分,我可以发布其余部分,但它似乎与问题无关。 DataNavigator 的 Search 参数(我相信)绑定到 VM 中的 SearchString,并且(我相信)绑定到 SearchDialog 的依赖属性。我可能会在这里混淆某些东西(对于 DP 来说是新手),所以如果我是,请提出我可以做出的改变。
  • 好的,我现在看到了。不确定这是否可行,因为您将字符串绑定到绑定。我认为搜索属性需要是一个字符串才能使该绑定起作用。

标签: silverlight mvvm silverlight-3.0 dependency-properties


【解决方案1】:

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

然后将 Navigator 的 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;
}

其余的和上面一样,我得到了我想要的效果。

【讨论】:

    【解决方案2】:

    不确定中间的 Binding 属性是否会起作用,因为您正在尝试将其绑定到字符串类型的属性。

    由于您已经在使用 Nikhil 的一些东西,您可能想看看 how he uses Tasks to handle dialogs/child windows,同时仍然保留 MVVM 范例。

    【讨论】:

      猜你喜欢
      • 2012-08-29
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 2019-05-26
      • 2011-04-11
      相关资源
      最近更新 更多