Xamarin.forms 动态加载时更改listview中控件的背景

Xamarin.forms 动态加载时更改listview中控件的背景,xamarin.forms,Xamarin.forms,我正在为我的项目使用xamarin.forms。我的列表视图如下所示: <ListView x:Name="lst_port" HasUnevenRows="True"> <ListView.ItemTemplate> <DataTemplate> <ViewCel

我正在为我的项目使用xamarin.forms。我的列表视图如下所示:

<ListView x:Name="lst_port" HasUnevenRows="True">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <Frame Style="{StaticResource FrmDashboard}" Margin="5">
                                            <StackLayout>
                                                <StackLayout.GestureRecognizers>
                                                    <TapGestureRecognizer Tapped="OnPortering_Tapped"></TapGestureRecognizer>
                                                </StackLayout.GestureRecognizers>
                                                <Grid>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition Height="*" />
                                                    </Grid.RowDefinitions>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="*" />
                                                    </Grid.ColumnDefinitions>
                                                    <Label Grid.Row="0" Grid.Column="0" Style="{StaticResource LISTTitleLabel}" Text="{Binding PorteringNumber}"></Label>
                                                    <Button Grid.Row="0" Grid.Column="0" Style="{StaticResource LISTTitleButton}" Text="{Binding PorteringStatus}" BackgroundColor="#2d964c"></Button>
                                                </Grid>
                                                <StackLayout Orientation="Horizontal">
                                                    <Image Style="{StaticResource LISTImageIcon}" Source="note.png"/>
                                                    <Label Style="{StaticResource LISTBodyLabel}" Text="{Binding PorteringAssetNumber}"></Label>
                                                </StackLayout>
                                                <StackLayout Orientation="Horizontal">
                                                    <Image Style="{StaticResource LISTImageIcon}" Source="clock.png"/>
                                                    <Label Style="{StaticResource LISTBodyLabel1}" Text="{Binding PorteringDate}"></Label>
                                                    <Image Style="{StaticResource LISTImageIcon}" Source="calender.png"/>
                                                    <Label Style="{StaticResource LISTBodyLabel1}" Text="{Binding PorteringTime}"></Label>
                                                </StackLayout>
                                            </StackLayout>
                                        </Frame>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

我使用代码隐藏数据表将值分配给listview。 结果如下图所示:

现在我想更改按钮的背景色,当绑定文本为“紧急”时。

有几个选项:

  • 决定ViewModel中表示
    ListView的按钮颜色。可以通过在ViewModel中引入附加属性来完成项
  • 您可以创建一个-将根据输入返回按钮颜色
  • 例如:
    input==“正常”?颜色.绿色:颜色.红色


    在这两种情况下,您都必须将所选解决方案绑定到
    按钮。
    列表视图中的BackgroundColor
    属性

    您可以使用触发器来实现这一点

    请尝试下面的代码

    <Button Grid.Row="0" Grid.Column="0" Style="{StaticResource LISTTitleButton}" Text="{Binding PorteringStatus}" BackgroundColor="#2d964c">
        <Button.Triggers>
            <DataTrigger TargetType="Label" Binding="{Binding PorteringStatus}" Value="Emergency">
                <Setter Property="BackgroundColor" Value="Red" />
            </DataTrigger>
        </Button.Triggers>
    </Button>