ReactiveUI BindTo不更新ReactiveList

时间:2016-12-06 08:19:04

标签: c# binding observablecollection reactive-programming reactiveui

我有一个名为ReactiveObject的{​​{1}}类:

GroupedIssueTypeSearchFilter

然后在public class GroupedIssueTypeSearchFilter : ReactiveObject { public int CategoryID { get; set; } public string CategoryTitle { get; set; } int selectionCode; public int SelectionCode { get { return selectionCode; } set { this.RaiseAndSetIfChanged(ref selectionCode, value); } } public ReactiveList<IssueTypeSearchFilter> IssueTypeFilterList { get; set; } public GroupedIssueTypeSearchFilter(int catID, string catTitle, List<IssueTypeSearchFilter> issueTypeList) { this.CategoryID = catID; this.CategoryTitle = catTitle; this.IssueTypeFilterList = new ReactiveList<IssueTypeSearchFilter>(issueTypeList); } } 类中,其中一个属性为IssueTypeFilterTableViewDelegate

GroupedIssueTypesFilter

我还有public ReactiveList<GroupedIssueTypeSearchFilter> GroupedIssueTypesFilter { get; set; } 及其ViewController。我使用ViewModel将其中一个视图模型属性绑定到该类:

BindTo

IssueTypeFilterTableViewDelegate tvd = new IssueTypeFilterTableViewDelegate(); this.WhenAnyValue(vm => vm.ViewModel.GroupedStandardIssueTypesTV) .BindTo(this, x => x.tvd.GroupedIssueTypesFilter); 类型为GroupedStandardIssueTypesTV

稍后,在ReactiveList<GroupedIssueTypeSearchFilter>内部进行了一些操作,ViewModel已更改

但是,GroupedStandardIssueTypesTV.IssueTypeFilterList未更改。我需要关闭View,重新打开它,然后它的值将被更新。

如何使GroupedIssueTypesFilter.IssueTypeFilterList跟随GroupedIssueTypesFilter中的更改?

2 个答案:

答案 0 :(得分:3)

完全同意@ramonesteban78 answer

但您可以采用其他方式,使用Clear和Add / AddRange方法重新填充ReactiveList。

像这样:

public GroupedIssueTypeSearchFilter(int catID, string catTitle, List<IssueTypeSearchFilter> issueTypeList)
{
    this.CategoryID = catID;
    this.CategoryTitle = catTitle;
    this.IssueTypeFilterList.Clear();
    this.IssueTypeFilterList.AddRange(issueTypeList);
}

不要忘记阅读“抑制通知”#39;在这里:https://docs.reactiveui.net/en/user-guide/lists/index.html

答案 1 :(得分:0)

我认为您的问题是您没有在ReactiveList中提出更改声明:

public ReactiveList<IssueTypeSearchFilter> IssueTypeFilterList { get; set; }

试试这个:

ReactiveList<IssueTypeSearchFilter> issueTypeFilterList
public ReactiveList<IssueTypeSearchFilter> IssueTypeFilterList 
{ 
    get { return  issueTypeFilterList; }; 
    set { this.RaiseAndSetIfChanged(ref issueTypeFilterList, value); }; 
}

如果没有RaiseAndSetIfChanged,您将无法与集合中的视图进行通信。

您还可以订阅ReactiveList公开的observable,如:

ItemsAdded
ItemsRemoved
ItemsMoved
Changed
etc

此处有更多信息:https://docs.reactiveui.net/en/user-guide/lists/index.html