我有一个简单的视图模型:
public class SampleCollectionViewModel : ReactiveObject
{
public SampleCollectionViewModel()
{
this.Countries = new ReactiveList<Country>();
this.Countries.Add(new Country { Name = "Andora" });
this.Countries.Add(new Country { Name = "Aruba" });
this.Countries.Add(new Country { Name = "Afganistan" });
this.Countries.Add(new Country { Name = "Algeria" });
this.Countries.Add(new Country { Name = "Argentina" });
this.Countries.Add(new Country { Name = "Armenia" });
this.Countries.Add(new Country { Name = "Antigua" });
//CountryNames.AddRange(this.Countries.Select(x => x.Name));
}
public ReactiveList<Country> Countries { get; set; }
//public ReactiveList<string> CountryNames { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和一个简单的视图:
public partial class SampleCollectionWindow : Window, IViewFor<SampleCollectionViewModel>
{
public SampleCollectionWindow()
{
InitializeComponent();
this.ViewModel = new SampleCollectionViewModel();
this.Bind(ViewModel, vm => vm.Countries, v => v.CountriesListBox.ItemsSource);
}
public SampleCollectionViewModel ViewModel { get; set; }
object IViewFor.ViewModel
{
get { return ViewModel; }
set { ViewModel = (SampleCollectionViewModel)value; }
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到:
exception Additional information: Can't two-way convert between ReactiveUI.ReactiveList``1[DemoReactiveUiWpf.SampleCollection.Country] and System.Collections.IEnumerable. To fix this, register a IBindingTypeConverter
如果我更改
this.Bind(ViewModel, vm => vm.Countries, v => v.CountriesListBox.ItemsSource);
为
this.OneWayBind(ViewModel, vm => vm.Countries, v => v.CountriesListBox.ItemsSource);
ListBox 没有任何反应
稍后我将扩展这个简单的演示,以提供一个命令和异步命令来向模型添加新的国家/地区。
删除ListBox/中选定的ListView项目。
尝试 ReactiveUI 提供的过滤、排序和其他有趣的操作。
但目前我还无法进行简单的绑定......
如何正确绑定ReactiveList ListBox+ ListBox/ListView并进行常见操作?ListViewReactiveList<Country>
谢谢你!
您绝对不能使用双向绑定来绑定到 ItemsSource - 正如错误消息所示,您不能保证某人设置ListBox.ItemsSource将使用ReactiveList<Country>. 这适用于我的机器:
this.OneWayBind(ViewModel, x => x.Countries, x => x.countries.ItemsSource);
Run Code Online (Sandbox Code Playgroud)
...ListBox 没有任何反应
我想知道您是否没有配置DataTemplate?ReactiveUI 使用使用的数据模板覆盖默认的数据模板ViewModelViewHost(并且由于您很可能没有为国家/地区注册视图,因此它会失败)。
更新:这是我绑定 SelectedItems 的方式,将其放入视图中:
this.WhenAnyValue(x => x.countries.SelectedItems)
.Select(list => list.Cast<Country>())
.BindTo(this, x => x.ViewModel.SelectedCountries);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4428 次 |
| 最近记录: |