在Listview中为特定行更改TextBlock的颜色 - uwp

时间:2018-02-02 07:40:37

标签: c# listview uwp uwp-xaml

我有一个列表视图,用于绑定类中的数据。

        <ListView x:Name="ItemListView" ScrollViewer.VerticalScrollBarVisibility="Auto"  Width="Auto"  HorizontalAlignment="Stretch">
            <ListView.ItemTemplate>
                <DataTemplate>
                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" >
                            <TextBlock  Text="{Binding Name}" TextAlignment="Left" FontSize="20"  Width="50"/>
                        </StackPanel>
                        <StackPanel >
                            <TextBlock x:Name="tb_age" Text="{Binding Age}" FontSize="20" TextAlignment="Center"/>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

private void LoadListView()
    {
        ItemDetails messageData = new ItemDetails();
        ItemListView.ItemsSource = messageData.Collection;
     }

public class ItemDetails

    {
        public ItemDetails()
        {

            MatchList item;

            item = new MatchList();
            item.Name = "A";
            item.Age = "10";
            Collection.Add(item);

            item = new MatchList();
            item.Name = "B";
            item.Age = "20";
            Collection.Add(item);

            item = new MatchList();
            item.Name = "A";
            item.Age = "30";
            Collection.Add(item);
       }
    }

现在我想在它的值等于10时更改TextBlock(tb_age)的forcolor。如何迭代每个单元格并获取它的文本来比较值?

2 个答案:

答案 0 :(得分:2)

只需编写一个转换器,将Age转换为某种颜色 https://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter(v=vs.110).aspx

<ListView x:Name="ItemListView" ScrollViewer.VerticalScrollBarVisibility="Auto"  Width="Auto"  HorizontalAlignment="Stretch">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" >
                <TextBlock  Text="{Binding Name}" TextAlignment="Left" FontSize="20"  Width="50"/>
            </StackPanel>
            <StackPanel >
                <TextBlock x:Name="tb_age"
                Text="{Binding Age}"
                      ForeGround={Binding Age, Converter={StaticResource AgeToColorConvert}}
                        FontSize="20"
                        TextAlignment="Center"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

转换 - 非常简单的实施:

    public override object Convert(
            object value,
            Type targetType,
            object parameter)
    {
        int iValue;
        if (int.TryParse(value.ToString(), out iValue))
        { if (iValue == 10) return Colors.Blue; }
        return Colors.Black;  
    }

答案 1 :(得分:1)

您可以install the Microsoft.Xaml.Behaviors.Uwp.Managed package from NuGet然后使用DataTriggerBehavior设置前景。

首先将名称空间声明添加到<Page>元素:

xmlns:Interactivity="using:Microsoft.Xaml.Interactivity" 
xmlns:Interactions="using:Microsoft.Xaml.Interactions.Core"

现在您可以像以下一样使用它:

<StackPanel>
    <Interactivity:Interaction.Behaviors>
        <Core:DataTriggerBehavior Binding="{Binding Age}" Value="10">
            <Core:ChangePropertyAction TargetObject="{Binding ElementName=tb_age}" 
                       PropertyName="Foreground" Value="Red" />
        </Core:DataTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
    <TextBlock x:Name="tb_age" Text="{Binding Age}" FontSize="20" TextAlignment="Center" />
</StackPanel>

请注意,您无法使用Style触发器,因为它们在UWP应用中不可用。

此解决方案具有100%基于XAML的优势,因此您无需修改​​C#代码本身,并且演示文稿保留在声明性XAML代码中。