简体   繁体   English

绑定到选项卡控件后,如何延迟加载视图模型属性?

[英]How can I lazy load my view model properties when bound to a tab control?

I'm using MVVM in my WPF application in a master/detail type scenario where I have a list of search results (the master). 我在主/详细类型方案中的WPF应用程序中使用MVVM,其中有搜索结果列表(主)。 When a user selects a result I call Refresh and pass the database key to load the details. 当用户选择结果时,我将调用Refresh并传递数据库密钥以加载详细信息。 The details is a view model with some complex properties. 详细信息是具有一些复杂属性的视图模型。 Each of these properties has a list of items (representing data in a database) along with some other ICommands and properties. 这些属性中的每一个都有一个项目列表(表示数据库中的数据)以及一些其他ICommands和属性。

My view has a tab control where each tab displays the data for one of these complex properties. 我的视图有一个选项卡控件,其中每个选项卡都显示这些复杂属性之一的数据。 I only want to load the list of the complex properties (call the LoadItems() method) if the tab item used to view the data is selected (visible?). 如果选择了用于查看数据的选项卡项(可见?),我只想加载复杂属性的列表(调用LoadItems()方法)。

Here is an abbreviated example: 这是一个简短的示例:

public class MyItem{
    public string Id{get;set;}
    public DateTime Timestamp{get;set;}
}

public class ComplexClass:INotifyPropertyChanged
{
    private Guid _key;
    private List<MyItem> _items;

    public string Name{get;set;}
    public List<MyItem> Items 
    {
        get{
            if (_items == null) LoadItems(_key);
            return _items;
        }
        set{
            if(Equals(value, _items) return;
            _item = value;
            OnPropertyChanged("Items");
        }
    }
    public void LoadItems(Guid key){
    ... //code to load Items from database based on the key.
    }
    public ComplexClass(string name, Guid key){
        Name = name;
        _key = key;
    }
}

public class MyViewModel:INotifyPropertyChanged
{
    private ComplexClass _currentItems;
    public ComplexClass CurrentItems 
    {
        get{return _currentItems;}
        set{
            if(Equals(value, _currentItems) return;
            _currentItem = value;
            OnPropertyChanged("CurrentItems");
        }
    }
    private ComplexClass _historyItems;
    public ComplexClass HistoryItems
    {
        get{return _historyItems;}
        set{
            if(Equals(value, _historyItems) return;
            _historyItems= value;
            OnPropertyChanged("HistoryItems");
        }
    }

    public void Refresh(Guid key){
        CurrentItems = new ComplexClass("Foo", key);
        HistoryItems = new ComplexClass("Bar", key);
    }
}

And here is some example XAML (assume the TabControl's DataContex is an instance of the ViewModel). 这是XAML的示例(假设TabControl的DataContex是ViewModel的实例)。

... 
<TabControl>
    <TabItem Header="Current Items"
        DataContext={Binding CurrentItems}>
        ...
        <ItemsControl ItemsSource={Binding Items}>
            ....
        </ItemsControl>
    </TabItem>
    <TabItem Header="History Items"
        DataContext={Binding HistoryItems}>
        ...
        <ItemsControl ItemsSource={Binding Items}>
            ....
        </ItemsControl>
    </TabItem>
</TabControl>

This works the first time. 这是第一次。 The HistoryItems.Items Getter doesn't get called until the "History Items" tab is selected. 在选择“历史记录项”选项卡之前,HistoryItems.Items Getter不会被调用。

But if I call Refresh on the ViewModel instance it calls the Items getter on both CurrentItems and HistoryItems regardless of which tab is selected. 但是,如果我在ViewModel实例上调用Refresh,则无论选择了哪个选项卡,它都会在CurrentItems和HistoryItems上调用Items getter。

Basically I want to improve performance by only loading the data needed. 基本上,我只想通过加载所需的数据来提高性能。 In most cases users will either scroll through search results (calling ViewModel.Refresh) looking at the current items or the history. 在大多数情况下,用户将滚动浏览搜索结果(调用ViewModel.Refresh)以查看当前项或历史记录。 I only want to load both if the user changes tabs. 如果用户更改选项卡,我只想加载两者。

Is there any way that after I call Refresh I can only call LoadItems for either CurrentItems or HistoryItems based on which tab is selected? 在调用“刷新”之后,是否有任何方法只能根据选择的选项卡为CurrentItems或HistoryItems调用LoadItems?

There's a IsSelected property on TabItem element. TabItem元素上有一个IsSelected属性。

Create two bool properties in your view model. 在视图模型中创建两个bool属性。 Bind the same property to both TabItem element's IsSelected property. 将相同的属性绑定到两个TabItem元素的IsSelected属性。

Call the LoadItems() in respective set method for the selected tab. 在所选标签的相应set方法中调用LoadItems()

Hope that solves your problem :-) 希望能解决您的问题:-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将视图模型的属性绑定到用作项控件源的集合的项的属性 - How can I bind properties of a view model to properties of items of a collection used as source for an items control 如何将我的自定义用户控件及其自定义视图 model 包含到我的 xaml 视图中? - How can I include my custom user control with its custom view model into my xaml view? 如何使用foreach将模型中的属性输入到视图中? - How can I get my properties from a Model into my View with a foreach? 如何修改我的视图模型以仅检查特定属性的验证? - How can I modify my view model to check validation for only specific properties? 使用ListView时,如何在Xamarin Forms中将自定义控件绑定设置为父视图模型属性? - How can I set custom control binding to parent view model property in Xamarin Forms when using a ListView? 如何在Linq查询中使用模型属性? - How can I use my model properties in a Linq query? 如何在viewmodel中检测模型中属性的更改? - How can I dectect changes in properties inside my model in viewmodel? 如何编写通用方法将任何表单添加到选项卡控件并在这些表单上设置属性? - How can I write a generic method to add any form to a tab control and set properties on those forms? OnPost之后如何在我的视图中保留模型? - How can I keep my model in my View after OnPost? 如何在我的视图中访问模型中的属性 - How can I access the attributes from my model in my view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM