使用 ReactiveUI 中的代码隐藏在 ListBox 内绑定数据 [英] Bind data inside ListBox using code-behind in ReactiveUI

查看:40
本文介绍了使用 ReactiveUI 中的代码隐藏在 ListBox 内绑定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 WPF 列表框:

I have WPF listbox:

<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); 在代码隐藏中绑定有助于重构.

I like the ability to bind ListBox by name from code behind using: this.OneWayBind(ViewModel, vm => vm.DownloadManager.FileDownloads, v => v.FileDownloads.ItemsSource); Having binding in code-behind helps with refactoring.

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

Is there any way to bind Url texbox inside the listbox using code-behind?

推荐答案

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

Is there any way to bind Url textbox inside the listbox using code-behind?

暂时没有.您可以像现在一样使用 XAML 绑定,也可以将数据模板放在 UserControls 中.

Not at the moment. You can either use XAML bindings like you're doing now, or you can put your data templates inside UserControls.

对这种有点麻烦的方法的安慰是,如果您注册数据模板 UserControls 并在它们上实现 IViewFor<TViewModel>:

A consolation to this somewhat more cumbersome approach, is that if you register your data template UserControls and implement IViewFor<TViewModel> on them:

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

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

Then, you can write the ListBox simply as:

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

此行会自动为您连接一个 DataTemplate:

This line will automatically wire up a DataTemplate for you:

this.OneWayBind(ViewModel, vm => vm.DownloadManager.FileDownloads, v => v.FileDownloads.ItemsSource);

这篇关于使用 ReactiveUI 中的代码隐藏在 ListBox 内绑定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆