1
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:ctrls="clr-namespace:Xama_Test.Controls;assembly=Xama_Test"
         x:Class="Xama_Test.FlipPage1">

    <StackLayout VerticalOptions="Center" HorizontalOptions="Center">

        <ctrls:Flip ItemsSource="{Binding Imgs}" HeightRequest="300" AutoPlay="True">
            <ctrls:Flip.ItemTemplate>
                <DataTemplate x:Name="Dtmp">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition Height="30" />
                        </Grid.RowDefinitions>
                        <Image x:Name="FlipImage" Source="{Binding Key}" Grid.RowSpan="2" Aspect="AspectFill" />
                        <Label Text="{Binding Value}" Grid.Row="1" BackgroundColor="#333333" Opacity="0.5" TextColor="White" VerticalOptions="Center" />
                    </Grid>
                </DataTemplate>
            </ctrls:Flip.ItemTemplate>
        </ctrls:Flip>
    </StackLayout>
</ContentView>

I make FlipView like this.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:controls="clr-namespace:CarouselView.FormsPlugin.Abstractions;assembly=CarouselView.FormsPlugin.Abstractions"              
         xmlns:local="clr-namespace:Xama_Test"
         x:Class="Xama_Test.Xama_Test"
         Title="MainPage">
    <ScrollView x:Name="_LargeScroll" Orientation="Vertical">
        <StackLayout Orientation = "Vertical" x:Name="SLO" >
            <local:FlipPage1 x:Name="FlipMenu" />   
        </StackLayout>
        </ScrollView>
     </ContentPage>

and I use this FlipVIew in mainActivity.

I want get control named FlipImage in mainActivity's c#. and add fuction if tab this image. how can I get control in DataTemplete?

D.A.KANG
  • 265
  • 2
  • 4
  • 12

1 Answers1

-1

You can't acces to any control by name in data template. Because every item in this data template will have this same name, then which item you want?

You can do it by finding at Visual tree.

using sample:

private void SomeMethod()
{
    ComboBox myCombo = GetVisualChild<ComboBox>(_contentPresenter);
}
private T GetVisualChild<T>(DependencyObject parent) where T : Visual
{
    T child = default(T);
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
        if (child != null)
        {
            break;
        }
    }
    return child;
}

Source: WPF How to access control from DataTemplate

Community
  • 1
  • 1
Niewidzialny
  • 330
  • 2
  • 18