Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xaml 如何基于xamarin表单中的bindig值更改listview行的颜色_Xaml_Xamarin_Xamarin.forms - Fatal编程技术网

Xaml 如何基于xamarin表单中的bindig值更改listview行的颜色

Xaml 如何基于xamarin表单中的bindig值更改listview行的颜色,xaml,xamarin,xamarin.forms,Xaml,Xamarin,Xamarin.forms,我有一个listview,其中有数据,在listview中有 1-taskname 2-日期 3-完成百分比 4-分配的任务 我想将条件放入列表中,并根据条件更改列表行的颜色,例如,如果完成百分比=0,则该行的颜色应为白色,如果百分比大于0则该行的颜色应为红色等 查看下面的图片: 在listview中,我有0%、7%和100%,所以我想知道它的0行颜色是否应该是白色,超过0行的颜色是否应该是b红色,如果它=100行的颜色是否应该是绿色,这是我的xaml代码 <ListView He

我有一个listview,其中有数据,在listview中有

1-taskname

2-日期

3-完成百分比

4-分配的任务

我想将条件放入列表中,并根据条件更改列表行的颜色,例如,如果完成百分比
=0
,则该行的颜色应为白色,如果百分比大于
0
则该行的颜色应为红色等

查看下面的图片:

在listview中,我有0%、7%和100%,所以我想知道它的0行颜色是否应该是白色,超过0行的颜色是否应该是b红色,如果它=100行的颜色是否应该是绿色,这是我的xaml代码

 <ListView   HeightRequest="20"  HasUnevenRows="True" SeparatorColor="Red" ItemsSource="{Binding GetAssignedTask}">
                    <ListView.ItemTemplate>
                        <DataTemplate>


                            <ViewCell >
                                <Grid  >
                                   <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>

                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Image Grid.Row="0" Grid.Column="0"  Source="rsz_pnglogocom.png"  />
                                    <Label Grid.Row="0" Grid.Column="1" Text="{Binding strTaskName}" TextColor="Black" FontSize="Medium" />
                                    <Label  Grid.Row="1" Grid.Column="1"  Text="{Binding intCurrCompletePercentage , StringFormat='{0}% Completed '}" Margin="0,0,10,0"  TextColor="Black" />

                                    <Label  Grid.Row="1" Grid.Column="2" Margin="0,0,20,0"  Text="{Binding dtStart,StringFormat='{0:MMMM dd, yyyy}'}"  TextColor="Red" HorizontalOptions="Center"/>

                                    <Label   Grid.Row="1" Grid.Column="3" Text="{Binding strAssignedByEmpName}" TextColor="Black" HorizontalOptions="End"/>
                                </Grid>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

对于本例,您还可以查看
DataTemplateSelector
。 查看文档

来自文档的代码

public class PersonDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate ValidTemplate { get; set; }
    public DataTemplate InvalidTemplate { get; set; }

    protected override DataTemplate OnSelectTemplate (object item, BindableObject container)
    {
        return ((Person)item).DateOfBirth.Year >= 1980 ? ValidTemplate : InvalidTemplate;
    }
 }
XAML


...
...
您必须制作3个模板(ZeroTemplate、UpperZeroTemplate、HundTemplate),并让您的
DataTemplateSelector
决定应该使用哪个模板

我只是发布了文档代码,因为我认为您应该经常自己做一些事情来学习一些东西,而不仅仅是复制粘贴:)

有两种方法可以做到这一点。 1.使用转换器:

创建值转换器:

public class BackgroundColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            if(value!=null)
            {
                if((int)value ==0)
                    return Color.White;
                else if((int)value > 0)
                    return Color.Red;
                else 
                    return Color.Green;
            }
        }
        catch (Exception ex)
        {
        }
        return null;
    }


    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
xaml部分:

xmlns:converter="RefrenceToConverterNameSpace"
<ContentPage.Resources>
    <ResourceDictionary>
        <converter:BackgroundColorConverter x:Key="BackgroundColorConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

<Grid BackgroundColor="{Binding intCurrCompletePercentage,Converter={StaticResource BackgroundColorConverter}}" />

每当intCurrCompletePercentage发生更改时,请不要忘记调用PercentColor的propertychanged事件。

有两种方法可以做到这一点。您可以使用值转换器,并根据您的值返回BackgroundColor,但如果您的值在同一页面上动态更改,因为转换器只触发一次,则此操作不起作用。因此,您可以在绑定对象中创建一个background属性,该属性根据您的值返回Color对象。
xmlns:converter="RefrenceToConverterNameSpace"
<ContentPage.Resources>
    <ResourceDictionary>
        <converter:BackgroundColorConverter x:Key="BackgroundColorConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

<Grid BackgroundColor="{Binding intCurrCompletePercentage,Converter={StaticResource BackgroundColorConverter}}" />
public Color PercentColor
{
    get
    {
        if(intCurrCompletePercentage ==0)
            return Color.White;
        else if(intCurrCompletePercentage > 0)
            return Color.Green;
        else 
            return Color.Red;
    }
}

<Grid BackgroundColor="{Binding PercentColor}" />