确保使用Picker Xamarin Forms

时间:2017-10-12 13:43:41

标签: xamarin xamarin.forms picker

我目前正在使用Xamarin Forms进行跨平台应用程序构建,我有几个选择器。我需要确保所有采摘者都有一个选定的项目,如果不是,我想将他们的背景设置为红色。 我已经尝试循环到stacklayout的每个元素来选择所有选择器并检查他们选择的项目,但它不起作用(似乎我的布局只有2个孩子而没有选择器)。我也看不出如何用行为来做这件事。

我的循环(在代码后面)

 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>

...

2 个答案:

答案 0 :(得分:1)

您可以使用triggers,并使用隐式Style来应用它们。

<TableView.Resources>
    <ResourceDictionary>
        <Style TargetType="Picker">
            <Setter Property="BackgroundColor" Value="Green" />
            <Style.Triggers>
                <Trigger TargetType="Picker" Property="SelectedItem" Value="{x:Null}">
                    <Setter Property="BackgroundColor" Value="Red" />
                </Trigger>
                <Trigger TargetType="Picker" Property="SelectedIndex" Value="0">
                    <Setter Property="BackgroundColor" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</TableView.Resources>

答案 1 :(得分:0)

您可以递归地通过保存Layout的最顶层Picker容器,并使用一些切换模式匹配来确定何时递归到下一个容器。

public void CheckPickers(Layout layout)
{
    foreach (var child in layout.Children)
    {
        switch (child)
        {
            case Picker picker:
                if (picker.SelectedIndex <= 0)
                    picker.BackgroundColor = Color.Red;
                else
                    picker.BackgroundColor = Color.Green;
                break;
            case Layout l:
                CheckPickers(l);
                break;
            default:
                break;
        }
    }
}