Hello,
Welcome to our Microsoft Q&A platform!
Get your control by FindByName<>
in your DataTemplate
code. It is not recommand way. You could get the control and set the value by Data Binding
.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/
Because they are in different NameScopes if you use this datatemplate
in the listview or collectionview. The Xaml parser creates one NameScope per Xaml root object in your file, and then different NameScopes for DataTemplates. Here is a similar thread:https://stackoverflow.com/a/42854892
Do you want to achieve the function that click the Image, then text of LabelPostGender
will be changed?
If so, I achieve simple code with data-binding.
First of all, I create a model called PlatformInfo.cs
, it achieve the INotifyPropertyChanged
for update at the runtime.
public class PlatformInfo : INotifyPropertyChanged
{
private bool _isChecked;
private string _platformName;
public bool IsChecked
{
get { return _isChecked; }
set { _isChecked = value; NotifyPropertyChanged(); }
}
public string PlatformName
{
get { return _platformName; }
set { _platformName = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Then I achieve a viewModel, that I add binding data and command. ChangeTextCommmand
will binding to the Image's TapGestureRecognizer
command. If this command is invoked, PlatformName's text will be changed.
public class ViewModel
{
public ViewModel()
{
this.GetContactsList();
}
public List<PlatformInfo> PlatformsList { get; set; }
public ICommand ChangeTextCommmand { get; set; }
private void GetContactsList()
{
if (this.PlatformsList == null)
this.PlatformsList = new List<PlatformInfo>();
this.PlatformsList.Add(new PlatformInfo() { IsChecked = true, PlatformName = "Android" });
this.PlatformsList.Add(new PlatformInfo() { IsChecked = true, PlatformName = "iOS" });
this.PlatformsList.Add(new PlatformInfo() { IsChecked = false, PlatformName = "UWP" });
ChangeTextCommmand=new Command((Myobject) =>
{
PlatformInfo platformInfo= Myobject as PlatformInfo;
platformInfo.PlatformName = "tes";
});
}
}
Here is test xaml layout.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:imagelongclickdemo="clr-namespace:ImageLongClickDemo"
x:Class="ImageLongClickDemo.MainPage">
<ContentPage.BindingContext>
<imagelongclickdemo:ViewModel />
</ContentPage.BindingContext>
<StackLayout x:Name="StackLayoutPostDetails" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BindableLayout.ItemsSource="{Binding PlatformsList}" >
<BindableLayout.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Grid.Row="1" Margin="0,10,5,5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image x:Name="myImage" Grid.Column="0" WidthRequest="30" Source="icon1.png" HeightRequest="30" Margin="5" HorizontalOptions="Center" VerticalOptions="End">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BindingContext.ChangeTextCommmand , Source={x:Reference Name=StackLayoutPostDetails}}" CommandParameter="{Binding .}" NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
<StackLayout x:Name="Mysl" Grid.Column="1" Orientation="Horizontal" Margin="5" >
<Label Text="Gender : " VerticalOptions="Center" />
<Label x:Name="LabelPostGender" Text="{Binding PlatformName}" VerticalOptions="Center" />
</StackLayout>
</Grid>
</Grid>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</ContentPage>
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.