【问题标题】:Loop into ListView elements循环进入 ListView 元素
【发布时间】:2019-10-18 19:09:29
【问题描述】:

我试图通过 ListView 循环,因为我想根据对象内容将“x:Name="lblEstatus”设置为不同的颜色。如何访问我的列表视图的数据模板元素以更改颜色?

  foreach (var item in myLista.ItemsSource)
   {
     //I loop the list with this, but how to access each viewcell ?
     // I tried to make this: to get the view but not possible
     // (ViewCell) test = ... 
   }

// 这是我的列表视图的 XAML 代码

        <ListView x:Name="myLista" HasUnevenRows="true" Margin="0,5,0,5"  SeparatorColor="LightGray"  VerticalOptions="FillAndExpand" RefreshControlColor="#30AAEA">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell Tapped="ViewCell_Tapped">
                        <StackLayout Orientation="Vertical" HeightRequest="105">
                            <StackLayout Orientation="Horizontal" HorizontalOptions="Center" Spacing="10" Margin="0,5,0,0">
                                <Label Text="EFO: " HorizontalOptions="Center" VerticalTextAlignment="Center"  HorizontalTextAlignment="Center"  TextColor="Black" FontSize="15" x:Name="lblClienProve"/>
                                <Label Text="{Binding sRFC}" HorizontalOptions="Center" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" TextColor="Black" FontSize="15"/>
                                <Label x:Name="lblEstatus" Text="|  Estatus:" HorizontalOptions="Center" VerticalTextAlignment="Center"  HorizontalTextAlignment="Center"  TextColor="Black" FontSize="15"/> // change this color depending object value
                                <Label Text="{Binding sEstatus}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" TextColor="Black" FontSize="15" Margin="5,0,5,0"/>
                            </StackLayout>
                            <BoxView  HeightRequest="2" WidthRequest="250" Color="LightSlateGray" HorizontalOptions="Center"/>
                            <StackLayout Orientation="Horizontal" HorizontalOptions="Center" Spacing="10" Margin="0,5,0,0">
                                <Label Text="Contribuyente: " HorizontalOptions="Center" VerticalTextAlignment="Center"  HorizontalTextAlignment="Center"  TextColor="Black" FontSize="15"/>
                                <Label Text="{Binding sContribuyente}" HorizontalOptions="Center" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" TextColor="Black" FontSize="15"/>
                            </StackLayout>

                            <StackLayout Spacing="50" HorizontalOptions="Center">
                                <StackLayout Orientation="Horizontal" HorizontalOptions="Center" Spacing="10" Margin="0,5,0,0">
                                    <Label Text="{Binding sNotifica}" HorizontalOptions="Center" VerticalTextAlignment="Center"  HorizontalTextAlignment="Center"  TextColor="Black" FontSize="15"/>
                                    <Label Text=" |  " HorizontalOptions="Center" VerticalTextAlignment="Center"  HorizontalTextAlignment="Center"  TextColor="Black" FontSize="15"/>
                                    <Label Text="{Binding sLeido}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" TextColor="Black" FontSize="15" Margin="5,0,5,0"/>
                                </StackLayout>
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

【问题讨论】:

    标签: c# android listview xamarin xamarin.forms


    【解决方案1】:

    您无法通过ItemsSource 访问单元格 - 您只有在单元格中显示的项目。做你想做的最惯用的方法是将颜色绑定到你的项目中。

    您可以通过在您的项目上设置一个返回正确颜色的属性或实现您自己的值转换器来做到这一点。转换器可能看起来像这样:

    public class TextToColorConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value switch
            {
                "value1" => Color.Black,
                "value2" => Color.Red,
                _ => Color.Blue
            };
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // 
        }
    }
    

    您可以在docs.microsoft.com 上阅读有关IValueConverter 的更多信息

    【讨论】:

    • 现在我绑定对象的颜色。这是最简单的方法,而且奏效了。谢谢,你给了我一个我没想到的观点
    【解决方案2】:

    关于foreach by items source的Idk,但您可以这样做以根据标签文本更改颜色

    <Label Text="{Binding sEstatus}">
     <Label.Triggers>
      <DataTrigger TargetType="Label" Binding="{Binding sEstatus}" Value="STATUS1">
       <Setter Property="TextColor" Value="Green"/>
      </DataTrigger>
      <DataTrigger TargetType="Label" Binding="{Binding sEstatus}" Value="STATUS2">
       <Setter Property="TextColor" Value="Red"/>
      </DataTrigger>  
     </Label.Triggers>
    </Label>
    

    【讨论】:

      【解决方案3】:

      首先,为 ListView 中的项目设置 x:Name 是个坏主意,而且不会起作用。

      其次,您必须根据内容设置标签的颜色。有两种方法可以做到这一点:

      1:IValueConverter:根据内容,可以返回TextColor。

      2:在您的模型中创建一个名为 TextColorProperty 的新属性,并添加适当的逻辑来为每个分配的对象更新此属性。 然后将其绑定到 TextColor 属性为TextColor={Binding TextColorProperty}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-06-14
        • 1970-01-01
        • 1970-01-01
        • 2021-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多