my content page is binded to a view model, but now I need to bind some components to a different view model. My problem is that because I have binded the BindingContext of the page to a view model, when I try binding elements to the second view model the {Binding propertyname} statement looks for the property in the view model that is binded to the bindingcontext.
so my question is this: How does one simply bind an element to a view model other then bindingcontext when bindingcontext is set to a different view model?
Things I tried that didn't work
code
<ContentPage.Resources>
<ResourceDictionary>
<selectedDeal:DealsMViewModel x:Name="SelectedDeal" x:Key="SelectedDeal"/>
</ResourceDictionary>
</ContentPage.Resources>
code
<Image
x:Name="CompanyImage"
HeightRequest="200"
Aspect="AspectFill"
Source="{Binding Source={StaticResource SelectedDeal}, Path=DealImage}" />
code
<Image
x:Name="CompanyImage"
HeightRequest="200"
Aspect="AspectFill"
Source="{Binding DealImage}"/>
There are two ways I made the binding work :
code
<Image
x:Name="CompanyImage"
HeightRequest="200"
Aspect="AspectFill"
Source="{Binding secondViewModel.DealImage}"/>
code
CompanyImage.Source = selectedDeal.DealImage;
But what I really want to achieve is to learn the way of doing this in the XAML part if possible! any help is much appreciated!
There are several way to achieve what you are asking... I'll explain the way I'm handling ViewModel bindings...
public class ViewModelLocator
{
public ViewModelLocator()
{
DependencyService.Register<ILogService, LogService>();
if (!DesignMode.IsDesignModeEnabled)
{
DependencyService.Register<IDataStore, MockDataStore>();
}
else
{
DependencyService.Register<IDataStore, DesignDataStore>();
}
DependencyService.Register<LoginViewModel>();
DependencyService.Register<AboutViewModel>();
DependencyService.Register<SettingsViewModel>();
}
public LoginViewModel Login => DependencyService.Get<LoginViewModel>();
public AboutViewModel About => DependencyService.Get<AboutViewModel>();
public SettingsViewModel Settings => DependencyService.Get<SettingsViewModel>();
}
Reference this class in your App.xaml
<viewModels:ViewModelLocator x:Name="Locator" x:Key="Locator" />
This will allow you to bind to any view model by just referencing the Locator. In the sample below, your "main" ViewModel is the LoginViewModel, but the button below is bound to the About ViewModel
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Test.Views.LoginPage"
BindingContext="{Binding Login, Source={StaticResource Locator}}"
Title="LoginPage">
<ScrollView>
<Grid Margin="10,20,10,10" RowDefinitions="Auto,Auto,Auto,Auto">
<Label Grid.Row="0" Text="Username:" TextColor="Black" FontSize="14"/>
<Entry Grid.Row="1" x:Name="usernameEditor" Text="{Binding Username}" ClearButtonVisibility="WhileEditing" />
<Label Grid.Row="2" Text="Password:" TextColor="Black" FontSize="14"/>
<Entry Grid.Row="3" x:Name="passwordEditor" Text="{Binding Password}" IsPassword="True" ClearButtonVisibility="WhileEditing" />
<Button Grid.Row="6" Text="Login" Command="{Binding About.LoginCommand, Source={StaticResource Locator}}"/>
</Grid>
</ScrollView>
</ContentPage>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.