简体   繁体   English

同一页面上的两个集合

[英]Two collections on the same page

I have a page that already has a DataContext. 我有一个已经有DataContext的页面。

When i change the pivot item, I need to bind another list to another collection. 当我更改数据透视表项时,我需要将另一个列表绑定到另一个集合。

How to achieve this? 如何实现呢?

Here is the first DataContext that shows first pivotitem info. 这是显示第一个枢纽项目信息的第一个DataContext。

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        if (NavigationContext.QueryString.TryGetValue("id", out _embarqueId))
        {
            String json = JsonConvert.SerializeObject(_embarqueId);

            using (IntrepService service = new IntrepService())
            {
                String retornojson = service.ObterDetalhesEmbarque(json);

                EmbarqueAtual = JsonConvert.DeserializeObject<EmbarqueViewModel>(retornojson);

                DataContext = EmbarqueAtual;
            }

            VerificaConclusao();
        }
    }

Then I try to load the second collection to the listbox, but doesn't work: 然后,我尝试将第二个集合加载到列表框中,但是不起作用:

private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (!_itemsareloaded && ((PivotItem)pivot.SelectedItem).Header.Equals("itens"))
        {
            using (IntrepService service = new IntrepService())
            {
                String json = JsonConvert.SerializeObject(_embarqueId);

                var retorno = service.ObterItensEmbarque(json);

                ItensDoEmbarque = JsonConvert.DeserializeObject<ObservableCollection<ItemDeEmbarqueViewModel>>(retorno);
                lstItens.DataContext = ItensDoEmbarque;
            }

        }
    }

You should have one ViewModel to hold all of your data that you want to bind to. 您应该有一个ViewModel来保存要绑定到的所有数据。 Set this ViewModel as your datacontext. 将此ViewModel设置为您的数据上下文。

public class ViewModel : INotifyPropertyChanged
{
    private ObservableCollection<ItemDeEmbarqueViewModel> _itensDoEmbarque;
    private EmbarqueViewModel _embarqueAtual;

    public ViewModel()
    {
        ItensDoEmbarque = new ObservableCollection<ItemDeEmbarqueViewModel>();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<ItemDeEmbarqueViewModel> ItensDoEmbarque
    {
        get { return _itensDoEmbarque; }
        set
        {
            _itensDoEmbarque= value;
            OnPropertyChanged("ItensDoEmbarque");
        }
    }

    public EmbarqueViewModel EmbarqueAtual
    {
        get { return _embarqueAtual; }
        set
        {
            _embarqueAtual = value;
            OnPropertyChanged("EmbarqueAtual");
        }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Within your OnNavigatedTo method set both of the properties and set the DataContext to be this object. 在您的OnNavigatedTo方法中,设置两个属性,并将DataContext设置为此对象。 You xaml would need to change to bind to the properties of these items instead of {Binding} 您的xaml需要进行更改以绑定到这些项目的属性,而不是{Binding}

You can set the collections that the PivotItem will be bound to ahead of time without worry of rendering delay. 您可以提前设置PivotItem绑定到的集合,而不必担心渲染延迟。 PivotItems delay rendering until they are shown. PivotItems延迟渲染,直到它们显示出来。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM