当前位置:   article > 正文

WPF DataGrid根据内容设置行颜色_wpf datagrid 行颜色

wpf datagrid 行颜色

转载:https://code.4noobz.net/wpf-change-color-of-a-row-in-a-datagrid-depending-on-the-value/

效果图

在这里插入图片描述

XAML代码

<Window x:Class="DataTrigger_Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid Margin="10"
                  ItemsSource="{Binding Users}" AutoGenerateColumns="False" ColumnWidth="*"
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch"
                  EnableRowVirtualization="false" EnableColumnVirtualization="false" 
                  CanUserAddRows="False" CanUserReorderColumns="False" CanUserResizeColumns="True">

            <DataGrid.CellStyle>
                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding FirstName}" Value="Dolores">
                            <Setter Property="Foreground" Value="Green" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding FirstName}" Value="Maeve">
                            <Setter Property="Foreground" Value="Blue" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.CellStyle>

            <DataGrid.Columns>
                <DataGridTextColumn Header="FirstName" Binding="{Binding FirstName}" MinWidth="150" />
                <DataGridTextColumn Header="LastName" Binding="{Binding LastName}" MinWidth="150" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

MVVM设置

using System.Windows;

namespace DataTrigger_Test
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowVM();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

注意使用了Prism MVVM框架

using System.Collections.ObjectModel;
using Microsoft.Practices.Prism.Mvvm;

namespace DataTrigger_Test
{
    public class MainWindowVM : BindableBase
    {
        private ObservableCollection<User> _users;
        public ObservableCollection<User> Users
        {
            get
            {
                return _users ?? (_users = new ObservableCollection<User>());
            }
            set
            {
                if (value != _users)
                {
                    _users = value;
                    OnPropertyChanged(() => Users);
                }
            }
        }

        public MainWindowVM()
        {
            Users.Add(new User {FirstName = "Dolores", LastName = "Abernathy"});
            Users.Add(new User {FirstName = "Maeve", LastName = "Millay"});       
        }
    }

    public class User
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/582848
推荐阅读
相关标签
  

闽ICP备14008679号