c# – 如何将ReactiveUI与分层数据源一起使用(树视图)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何将ReactiveUI与分层数据源一起使用(树视图),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3959字,纯文字阅读大概需要6分钟。
内容图文
我已经找到了一种通过ReactiveUI动态绑定树视图中的用户控件的方法.
但……
对HierachicalDataSource的顶级绑定是在XAML中而不是后面的代码,我需要直接设置ItemsSource,而不是根据ReactiveUI绑定的一般模式使用this.OneWayBind.
所以,我的问题是:我是否遗漏了ReactiveUI框架中的一些东西,让我绑定this.OneWayBind并将HierachicalDataTemplete移动到后面的代码或自定义用户控件?
特别是 – OneWayBind是否有另一个支持分层数据模板的重载,或者在使用时抑制数据模板生成的方法?
更新
我已经为我的测试项目添加了所选项目和Expand和Selected的编程支持,但我不得不为XAML添加一个样式.我想用一个简单的RxUI Bind替换它.更新了示例.
以下是关键细节:
主视图中的树控制
<TreeView Name="FamilyTree" >
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
</Style>
<HierarchicalDataTemplate DataType="{x:Type local:TreeItem}" ItemsSource="{Binding Children}">
<reactiveUi:ViewModelViewHost ViewModel="{Binding ViewModel}"/>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
主视图代码背后
public partial class MainWindow : Window, IViewFor<MainVM>
{
public MainWindow()
{
InitializeComponent();
//build viewmodel
ViewModel = new MainVM();
//Register views
Locator.CurrentMutable.Register(() => new PersonView(), typeof(IViewFor<Person>));
Locator.CurrentMutable.Register(() => new PetView(), typeof(IViewFor<Pet>));
//NB. ! Do not use 'this.OneWayBind ... ' for the top level binding to the tree view
//this.OneWayBind(ViewModel, vm => vm.Family, v => v.FamilyTree.ItemsSource);
FamilyTree.ItemsSource = ViewModel.Family;
}
...
}
MainViewModel
public class MainVM : ReactiveObject
{
public MainVM()
{
var bobbyJoe = new Person("Bobby Joe", new[] { new Pet("Fluffy") });
var bob = new Person("Bob", new[] { bobbyJoe });
var littleJoe = new Person("Little Joe");
var joe = new Person("Joe", new[] { littleJoe });
Family = new ReactiveList<TreeItem> { bob, joe };
_addPerson = ReactiveCommand.Create();
_addPerson.Subscribe(_ =>
{
if (SelectedItem == null) return;
var p = new Person(NewName);
SelectedItem.AddChild(p);
p.IsSelected = true;
p.ExpandPath();
});
}
public ReactiveList<TreeItem> Family { get; }
...
}
TreeItem基类
public abstract class TreeItem : ReactiveObject
{
private readonly Type _viewModelType;
bool _isExpanded;
public bool IsExpanded
{
get { return _isExpanded; }
set { this.RaiseAndSetIfChanged(ref _isExpanded, value); }
}
bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set { this.RaiseAndSetIfChanged(ref _isSelected, value); }
}
private TreeItem _parent;
protected TreeItem(IEnumerable<TreeItem> children = null)
{
Children = new ReactiveList<TreeItem>();
if (children == null) return;
foreach (var child in children)
{
AddChild(child);
}
}
public abstract object ViewModel { get; }
public ReactiveList<TreeItem> Children { get; }
public void AddChild(TreeItem child)
{
child._parent = this;
Children.Add(child);
}
public void ExpandPath()
{
IsExpanded = true;
_parent?.ExpandPath();
}
public void CollapsePath()
{
IsExpanded = false;
_parent?.CollapsePath();
}
}
人类
public class Person : TreeItem
{
public string Name { get; set; }
public Person(string name, IEnumerable<TreeItem> children = null)
: base(children)
{
Name = name;
}
public override object ViewModel => this;
}
人查看用户控件
<UserControl x:Class="TreeViewInheritedItem.PersonView"... >
<StackPanel>
<TextBlock Name="PersonName"/>
</StackPanel>
</UserControl>
人视图代码背后
public partial class PersonView : UserControl, IViewFor<Person>
{
public PersonView()
{
InitializeComponent();
this.OneWayBind(ViewModel, vm => vm.Name, v => v.PersonName.Text);
}
...
}
宠物的工作和人一样.
整个项目在这里ReactiveUI Tree view Sample
解决方法:
我查看了ReactiveUI源代码,这是执行此操作的唯一方法.绑定帮助器方法始终使用DataTemplate而不是HierarchicalDataTemplate.
因此,此方法将使用XAML绑定进行最高级别,然后让您在所有TreeView项目上使用ReactiveUI绑定.
我将看到创建一个拉请求来处理这个边缘情况.
谢谢,
克里斯
内容总结
以上是互联网集市为您收集整理的c# – 如何将ReactiveUI与分层数据源一起使用(树视图)全部内容,希望文章能够帮你解决c# – 如何将ReactiveUI与分层数据源一起使用(树视图)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。