【问题标题】:Change the background of control in listview when dynamically loading动态加载时更改listview中控件的背景
【发布时间】:2023-03-22 01:11:01
【问题描述】:

我正在为我的项目使用 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>

我使用数据表后面的代码将值分配给列表视图。 结果如下图所示:

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

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

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

    请尝试以下代码。

    <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>
    

    【讨论】:

      【解决方案2】:

      有几个选项:

      1. 决定 ViewModel 中代表 ListView.Item 的按钮颜色 - 可以通过在 ViewModel 中引入一个附加属性来完成。
      2. 您可以创建一个IValueConverter - 它将根据输入返回按钮颜色。

      例如:input == "Normal" ? Color.Green : Color.Red

      在这两种情况下,您都必须将所选解决方案绑定到 ListView 内的 Button.BackgroundColor 属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-30
        相关资源
        最近更新 更多