2017-04-01 7 views
1

現在、私はXamarinに基づいてアプリケーションをプログラミングしています。私の目標は、テーブル内の値に基づいてスイッチを切り替えることです。問題は、Xamarinは私のx:Name toggle_trueが私のスイッチに接続されていることに気づいていません。トグルできませんスイッチ

はここにXAMLで私のスクリプトです:名前defineBuildingItems:それは、xがどのように認識するか

var currentQuestions = _define.Where(s => DefineId.Contains(s.defineId)); 

     //Remember what building was defined like last time defineBuilding was proceeded. 
     foreach (DefineTable i in currentQuestions) 
     { 

      toggled_true.IsToggled = true; 

     } 

     defineBuildingItems.ItemsSource = posts; 
     base.OnAppearing(); 
    } 

お知らせ:

<ListView x:Name="defineBuildingItems"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 

        <ViewCell> 

          <StackLayout Orientation="Horizontal"> 
          <StackLayout Orientation="Vertical"> 
           <Label Text="{Binding defineDescription}" x:Name="Current_item"/> 

          </StackLayout> 

           <Switch BindingContext ="{Binding defineId}" HorizontalOptions="EndAndExpand" 
             Toggled="Handle_Toggled" x:Name="toggled_true"/> 

          </StackLayout> 

        </ViewCell> 

       </DataTemplate> 
      </ListView.ItemTemplate> 
      </ListView> 

はここ背後にあるコードで私のC#コードです。 toggled_trueのエラーは次のようなものです。名前toggled_trueが現在の名前空間に存在しません。私が間違っているか紛失していることはありますか?

答えて

1

datatemplate内の名前でコントロールにアクセスできません。あなたはバインディングを使用する必要があります。これは単に一例です:

モデル:背後

public class Data 
{ 
    public bool IsToggled {get;set;} 
    .... 
} 

コード:

​​

あなたのXAML:

<ListView x:Name="defineBuildingItems" ItemSources={Binding Items}> 
    <ListView.ItemTemplate> 
     <DataTemplate> 

      <ViewCell> 

        <StackLayout Orientation="Horizontal"> 
        <StackLayout Orientation="Vertical"> 
         <Label Text="{Binding defineDescription}"  /> 

        </StackLayout> 

         <Switch IsToggled ="{Binding IsToggled, Mode =TwoWay}" HorizontalOptions="EndAndExpand" /> 

        </StackLayout> 

      </ViewCell> 

     </DataTemplate> 
    </ListView.ItemTemplate> 
    </ListView> 
関連する問題