如何在条件下更改ListView中TextCell的textcoroul或背景颜色

时间:2016-12-10 10:31:58

标签: c# android windows-phone-8 xamarin xamarin.forms

我想知道,如何按条件更改列表视图中的textcell颜色。如果它是背景颜色或文本颜色无关紧要,我只想根据条件突出显示一些行。我的代码示例如下。

  <ListView x:Name="lstData" HasUnevenRows="false" Footer="Footer" ItemSelected="OnSelection" >
    <ListView.ItemTemplate>
      <DataTemplate>
        <TextCell Text="{Binding OrderId}" Detail="{Binding OrderState}" />
      </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.FooterTemplate>
      <DataTemplate>
        <StackLayout Orientation="Horizontal" Padding="5,5,5,5">
          <Button Text="New Order" Clicked="OnNewOrderClicked" />
        </StackLayout>
      </DataTemplate>
    </ListView.FooterTemplate>
  </ListView>

    public ManageOrder()
    {
        InitializeComponent();
        var vList = App.orderDatabase.GetAllOrders();
        lstData.ItemsSource = vList;

        for( int i=0; i < vList.Count(); i++ )
        {
            if( vList[i].IsDispatched == false )
            {
        // **Change colour of text view**
            }

        }        
    }

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

 <ListView x:Name="lstData" HasUnevenRows="false" Footer="Footer" ItemSelected="OnSelection" >
    <ListView.ItemTemplate>
      <DataTemplate>
        <TextCell Text="{Binding OrderId}" Detail="{Binding OrderState}" TextColor= "{Binding StateColor}" />
      </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.FooterTemplate>
      <DataTemplate>
        <StackLayout Orientation="Horizontal" Padding="5,5,5,5">
          <Button Text="New Order" Clicked="OnNewOrderClicked" />
        </StackLayout>
      </DataTemplate>
    </ListView.FooterTemplate>
  </ListView>

在订单(这是你的vlist)中,我将拥有一个属性为Color的属性(xamarin.forms库中的那个),你可以控制它。

答案 2 :(得分:0)

You can achieve your requirement by using IValueConverter.

    <ContentPage.Resources>
        <ResourceDictionary>
            <local:Class1 x:Key="class1" />
        </ResourceDictionary>
    </ContentPage.Resources>

<ListView x:Name="lstData">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding OrderId}" TextColor="{Binding Colors, Converter={StaticResource class1}}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

//Converter class

    public class Class1 : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((int)value > 1000)
                return Color.Green;
            else
                return Color.Red;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }