2017-03-13 1 views
0

コードビハインドfirstGridおよびsecondGridオブジェクトから回復します。 しかし、これらはコードビハインドから呼び出すことができるので、DataTemplateの内部にありますか?XamarinフォームのDataTemplate内でcodebehindから変数を呼び出す

ありがとうございます。

<control:CarouselView x:Name="carouselView"> 
          <control:CarouselView.ItemTemplate> 
           <DataTemplate> 
            <Grid> 
             <Grid.ColumnDefinitions> 
              <ColumnDefinition Width="*"></ColumnDefinition> 
             </Grid.ColumnDefinitions> 
             <Grid.RowDefinitions> 
              <RowDefinition Height="46*"></RowDefinition> 
              <RowDefinition Height="0.2*"></RowDefinition> 
              <RowDefinition Height="53.8*"></RowDefinition> 
             </Grid.RowDefinitions> 
             <!-- griglia del carousel, tutta la struttura inizia qui--> 
             <Grid Grid.Row="0"> 
              <Grid.ColumnDefinitions> 
               <ColumnDefinition Width="10*"></ColumnDefinition> 
               <ColumnDefinition Width="35*"></ColumnDefinition> 
               <ColumnDefinition Width="45*"></ColumnDefinition> 
               <ColumnDefinition Width="10*"></ColumnDefinition> 
              </Grid.ColumnDefinitions> 
              <Grid.RowDefinitions> 
               <RowDefinition Height="*"></RowDefinition> 
              </Grid.RowDefinitions> 
              <Button Image="backBlueArrow.png" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="clickmePrev" Grid.Column="0"></Button> 
              <!--<Image Source="backBlueArrow.png" x:Name="backClickArrow" Aspect="AspectFit" Grid.Column="0"></Image>--> 
              <StackLayout Margin="0,0,10,0" VerticalOptions="Center" HorizontalOptions="End" Grid.Column="1"> 
               <Image HeightRequest="70" Source="{Binding Immagine}" Aspect="AspectFit"></Image> 
              </StackLayout> 
              <!-- griglia superiore, riprende i dati tra le barre bianche--> 
              <Grid Grid.Row="0" Grid.Column="2"> 
               <Grid.ColumnDefinitions> 
                <ColumnDefinition Width="*"></ColumnDefinition> 
               </Grid.ColumnDefinitions> 
               <Grid.RowDefinitions> 
                <RowDefinition Height="40*"></RowDefinition> 
                <RowDefinition Height="60*"></RowDefinition> 
               </Grid.RowDefinitions> 
               <Label Text="{Binding Descrizione}" Margin="{Binding Margine}" TextColor="#fff" FontSize="Small" VerticalTextAlignment="End" HorizontalTextAlignment="Start" Grid.Row="0"/> 
               <Label Text="{Binding Valore}" FontSize="Large" TextColor="#fff" VerticalTextAlignment="Start" HorizontalTextAlignment="Start" Grid.Row="1"/> 
               <Image Margin="-25,-5,0,0" Source="{Binding ImmagineBatteriaEmoticon}" HeightRequest="50" x:Name="emoticonBattery" Grid.Row="1" VerticalOptions="Start" HorizontalOptions="Start"></Image> 
              </Grid> 
              <Button Image="nextBlueArrow.png" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="clickmeNext" Grid.Column="3"></Button> 
              <!--<Image Source="nextBlueArrow.png" x:Name="nextClickArrow" Aspect="AspectFit" Grid.Column="3"></Image>--> 
             </Grid> 
             <!-- griglia fare la riga bianca --> 
             <Grid BackgroundColor="White" Grid.Row="1"> 
              <Grid.ColumnDefinitions> 
               <ColumnDefinition Width="*"></ColumnDefinition> 
              </Grid.ColumnDefinitions> 
              <Grid.RowDefinitions> 
               <RowDefinition Height="*"></RowDefinition> 
              </Grid.RowDefinitions> 
             </Grid> 
             <!-- griglia inferiore, ritrare i dati dei testi --> 
             <ScrollView Grid.Row="2"> 
              <Grid Padding="5" Grid.Row="2" x:Name="firstGrid"> 
               <Grid.ColumnDefinitions> 
                <ColumnDefinition Width="65*"></ColumnDefinition> 
                <ColumnDefinition Width="35*"></ColumnDefinition> 
               </Grid.ColumnDefinitions> 
               <Grid.RowDefinitions> 
                <RowDefinition Height="*"></RowDefinition> 
                <RowDefinition Height="*"></RowDefinition> 
                <RowDefinition Height="*"></RowDefinition> 
                <RowDefinition Height="*"></RowDefinition> 
                <RowDefinition Height="*"></RowDefinition> 
                <RowDefinition Height="*"></RowDefinition> 
                <RowDefinition Height="*"></RowDefinition> 
                <RowDefinition Height="*"></RowDefinition> 
               </Grid.RowDefinitions> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="0" Text="{Binding Testo1}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="0" Text="{Binding Valore1}"></Label> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="1" Text="{Binding Testo2}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="1" Text="{Binding Valore2}"></Label> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="2" Text="{Binding Testo3}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="2" Text="{Binding Valore3}"></Label> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="3" Text="{Binding Testo4}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="3" Text="{Binding Valore4}"></Label> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="4" Text="{Binding Testo5}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="4" Text="{Binding Valore5}"></Label> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="5" Text="{Binding Testo6}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="5" Text="{Binding Valore6}"></Label> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="6" Text="{Binding Testo7}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="6" Text="{Binding Valore7}"></Label> 
               <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="7" Text="{Binding Testo8}"></Label> 
               <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="7" Text="{Binding Valore8}"></Label> 
              </Grid> 
              <RelativeLayout Grid.Row="2" x:Name="secondGrid" BackgroundColor="Fuchsia" HeightRequest="50" WidthRequest="100"> 
               <Label Text="ciao"></Label> 
              </RelativeLayout> 
             </ScrollView> 
            </Grid> 
            <!--<Label Text="{Binding Testo}" />--> 
           </DataTemplate> 
          </control:CarouselView.ItemTemplate> 
         </control:CarouselView> 

警告:

は、ここに私のコードであることが複製される前に、コードはXamarinは、PCLを形成するためにあります。 WPFでC#と少し違います。

おかげ

+0

グリッドで何をしたいですか?多分私たちは回避策を見つけることができます – Daniel

+0

私はカルーセルのインデックスに従ってそれを隠すために彼女に電話をしなければなりません。 –

+0

viewModelにプロパティisVisibleを追加し、それに応じて設定します – Daniel

答えて

1

あなたはDataTemplateを、結合の懸念あなたが使用している可能性がありViewModelにバインドとされていないオブジェクトを使用します。

Xamarinはこの種のオブジェクトに治療法を付ける方法を提供しているので、恐れはありません。

のは、あなたのイメージをクリックしましょう:私がやった場合

public async void clickmeNext(object sender, EventArgs e) 
     { 
      Image imageSender = (Image)sender; 
      YourObject yourObject= (YourObject)imageSender.BindingContext; 

      //some actions on your instance of YourObject, 
      //which is the instance linked to the image 
     } 

<Button Image="nextBlueArrow.png" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="clickmeNext" Grid.Column="3"></Button> 

はこのような背後にあるコードでclickmeNext方法で

<Image Source="nextBlueArrow.png"> 
    <Image.GestureRecognizers> 
    <TapGestureRecognizer 
     Tapped="clickmeNext" 
     CommandParameter="{Binding .}" /> 
    </Image.GestureRecognizers> 
</Image> 

になることがありあなたの質問に答えていない、あなたの質問にそれをもっと明確にするために追加することをためらうことはありません。

+0

ここで彼はViewModelではなくコードの背後にあると話していましたが、あなたが示した方法はうまくいくはずです... :) –

+0

このイベントは、 –

関連する問題