我目前正在使用Xamarin Forms創建一個跨平臺的應用程序,並且我有幾個選擇器。我需要確保所有采購員都有一個選定的項目,如果不是,我想將他們的背景設置爲紅色。 我已經嘗試循環到stacklayout的每個元素來選擇所有的選擇器,並檢查他們選擇的項目,但它不工作(這似乎是我的佈局只有2個孩子,沒有采摘)。我不知道如何用行爲來做到這一點。確保使用Picker Xamarin Forms選擇一個項目
我環路(在後面的代碼)
public void checkChampsVides()
{
for (int i = 0; i < DiagHabitat.Children.Count(); i++)
{
DisplayAlert("e", DiagHabitat.Children.GetHashCode().ToString(), "ok");
if (DiagHabitat.Children.ElementAt(i).GetType() == typeof(Picker))
{
Picker p = DiagHabitat.Children.ElementAt(i) as Picker;
if (p.SelectedIndex == 0)
p.BackgroundColor = Color.Red;
}
}
}
的XAML
<ContentPage
Title="Diagnostic Habitat"
Padding="20"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XXX.DiagnosticHabitatPage">
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key= "BoutonSauvegarde" TargetType="Button" >
<Setter Property="BackgroundColor" Value="#6AD0C6"/>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout x:Name="DiagHabitat">
<ProgressBar Progress="1"/>
<TableView x:Name ="DiagnosticHabitat" Intent="Form" HasUnevenRows="True">
<TableRoot Title="Diagnostic habitat">
<TableSection Title="Title1">
<ViewCell>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" >
<Label VerticalOptions="Center" Text="text1"/>
<Picker x:Name="accesPorteEntree" HorizontalOptions="FillAndExpand" SelectedIndex="{Binding DiagHabitatAjoute.AccesPorteEntreeEPC, Mode=OneWayToSource}" >
<Picker.Items>
<x:String>Seuil haut</x:String>
<x:String>Seuil bas</x:String>
<x:String>Sans seuil</x:String>
</Picker.Items>
</Picker>
</StackLayout>
</ViewCell>
<ViewCell>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" >
<Label VerticalOptions="Center" Text="text2"/>
<Picker x:Name="niveauSecuAcces" HorizontalOptions="FillAndExpand" SelectedIndex="{Binding DiagHabitatAjoute.SecuAccesEPC, Mode=OneWayToSource}">
<Picker.Items>
<x:String>Bas</x:String>
<x:String>Moyen</x:String>
<x:String>Haut</x:String>
</Picker.Items>
</Picker>
</StackLayout>
</ViewCell>
<ViewCell>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" >
<Label VerticalOptions="Center" Text="Largeur circulation"/>
<Picker x:Name="largeurCirculation" HorizontalOptions="FillAndExpand" SelectedIndex="{Binding DiagHabitatAjoute.LargeurCircuEPC, Mode=OneWayToSource}" >
<Picker.Items>
<x:String>Inf à 75 cm</x:String>
<x:String>75 - 90 cm</x:String>
<x:String>Sup à 90 cm</x:String>
</Picker.Items>
</Picker>
</StackLayout>
</ViewCell>
...
DiagHabitat只有2個直接的孩子,一個ProgressBar和一個表。爲什麼說明只需按名稱檢查每個選擇器? – Jason
謝謝你的回答。它只是代碼的摘錄,我有大約30個採集器,所以我正在尋找更優化的解決方案... –