WPF之DataGrid控件根據某列的值設置行的前景色(色


一種方法是 使用 datagrid的LoadingRow事件:

  private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            Employee model = e.Row.Item as Employee;
            if (model!=null)
            {
                if (model.Age<21)
                {
                    e.Row.Foreground = new SolidColorBrush(Colors.Blue);
                }
            }
        }

 

這種方法的缺點是只有在加載數據或新增數據時才起效果。

 

第二種方法就是用 行的樣式(RowStyle)+轉換器:

轉換器類:

 //定義值轉換器
    [ValueConversion(typeof(int), typeof(Brush))]
    public class IntToBrushConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int reValue = System.Convert.ToInt32(value);
            if (reValue == 1)
            {
                return new SolidColorBrush(Colors.Red);
            }
            else
            {
                return new SolidColorBrush(Colors.Black);
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string strValue = value.ToString();
            return value;
        }


    }

 

聲明轉換器類:

    <Window.Resources>
        <local:IntToBrushConvert x:Key="IntToBrushConvert"/>
    </Window.Resources>

 

DataGrid的行樣式已經綁定轉換器:

<DataGrid HeadersVisibility="Column" ItemsSource="{Binding Path=Employees}"  
                        SelectedItem="{Binding Path=SelectedEmployee}" CanUserAddRows="False" IsReadOnly="True"
                        AutoGenerateColumns="False">
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="Foreground" 
        Value="{Binding Path=Flag,Converter={StaticResource ResourceKey=IntToBrushConvert}}"></Setter>
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.Columns>
                <DataGridTextColumn  Header="工號" Binding="{Binding Path=EmployeeNum}" />
                <DataGridTextColumn  Header="名稱" Binding="{Binding Path=EmployeeName}" />
                <DataGridTextColumn  Header="職位" Binding="{Binding Path=Title}" />
                <DataGridTextColumn  Header="年齡" Binding="{Binding Path=Age}" />
                <DataGridTextColumn  Header="狀態" Binding="{Binding Path=Status,Converter={StaticResource ResourceKey=IntToStringConvert}}"/>
            </DataGrid.Columns>
        </DataGrid>

 

當Age屬性大於22時,把Flag屬性賦值為1:

 private int m_age;
        /// <summary>
        /// 年齡
        /// </summary>
        public int Age
        {
            get { return m_age; }
            set
            {
                if (value != m_age)
                {
                    m_age = value;
                    if (m_age > 22)
                    {
                        Flag = 1;
                    }
                    else
                    {
                        Flag = 0;
                    }
                    OnPropertyChanged("Age");
                }
            }
        }

 

運行截圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM