Select to view content in your preferred language

how to get mulitple model views in a single view xamarin crossplateform.

3627
1
10-02-2017 10:18 PM
NadirHussain
Frequent Contributor

Dear All,

i have a question.I have one model view for picker and other model view for datagrid.i want to use both in a single view.if

i use like below example

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:Picker="clr-namespace:TestApp.ViewModels;asembly=TestApp"
xmlns:bindPicker="clr-namespace:Xamarin.Forms.BindablePicker;assembly=Xamarin.Forms.BindablePicker"
xmlns:behaviors="clr-namespace:Behaviors;assembly=Behaviors"
 xmlns:DGViewModel="clr-namespace:TestApp.ViewModels.DataGrid"
xmlns:ViewModels="clr-namespace:TestApp.ViewModels.PickerViewModels"
xmlns:dg="clr-namespace:Xamarin.Forms.DataGrid;assembly=Xamarin.Forms.DataGrid"
x:Class="TestApp.Views.OtherPages.Querypage">

<ContentPage.BindingContext>
<ViewModels:ClsRegionPicker></ViewModels:ClsRegionPicker>
<DGViewModel:ClsDataGridViewModel></DGViewModel:ClsDataGridViewModel>
</ContentPage.BindingContext>

</ContentPage>

it only takes view model for picker and through error for datagrid.

one way is that i merge in one viewmodel.But i want to keep both in seperate viewmodels.

pls help.

Thanks in advance

0 Kudos
1 Reply
RichZwaap
Frequent Contributor

You don't have to set the BindingContext at the page level.  You can limit it to the control you want to set the binding on, or you can bind directly into the property and not use the BindingContext at all.  So you could have something like:

<ContentPage ...>

  <ContentPage.Resources>

    <ViewModels:ClsRegionPicker x:Key="RegionPickerViewModel" />

    <DGViewModel:ClsDataGridViewModel x:Key="ClsDataGridViewModel">

  </ContentPage.Resources>

  <Picker BindingContext="{StaticResource RegionPickerViewModel}" />

  <DataGrid BindingContext="{StaticResource ClsDataGridViewModel}" />

</ContentPage>

Notice that the view-models are declared as resources, and then the BindingContext for the Picker and DataGrid are specified separately.

0 Kudos