使用ReactiveUI中的代码隐藏将ListBox中的数据绑定

时间:2014-08-23 20:49:48

标签: reactiveui

我有WPF列表框:

<ListBox Name="FileDownloads" SelectionMode="Extended">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <TextBlock Name="Url" Text="{Binding Url}" />
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

我喜欢使用以下代码从名称绑定ListBox的能力: this.OneWayBind(ViewModel, vm => vm.DownloadManager.FileDownloads, v => v.FileDownloads.ItemsSource);在代码隐藏中绑定有助于重构。

有没有办法使用代码隐藏绑定列表框中的Url texbox?

1 个答案:

答案 0 :(得分:4)

  

有没有办法使用代码隐藏绑定列表框中的Url文本框?

目前不是。您可以像现在一样使用XAML绑定,也可以将数据模板放在UserControls中。

对这种稍微麻烦的方法的一种安慰是,如果您注册数据模板UserControls并在其上实现IViewFor<TViewModel>

Splat.Locator.CurrentMutable.Register(typeof(MyView), typeof(IViewFor<MyViewModel>));

然后,您可以将ListBox简单地写为:

<ListBox Name="FileDownloads" SelectionMode="Extended" />

此行将自动为您连接DataTemplate:

this.OneWayBind(ViewModel, vm => vm.DownloadManager.FileDownloads, v => v.FileDownloads.ItemsSource);
相关问题