Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

WPF How to make CONVERTER in a DataGrid with DataGridTextColumn created in code

$
0
0

I create datagridtextcolumn by code dynamicly. But in these way, the converter cannot work correctly.

<Window x:Class="MethodConfiguration.ShowTable"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MethodConfiguration;assembly="
        Title="ShowTable" Height="372" Width="892" Loaded="Window_Loaded"><Window.Resources><local:DataSourceConvert x:Key="dataSourceConvert"/></Window.Resources><Grid x:Name="gridMain"><DataGrid x:Name="dataGrid" AutoGenerateColumns="True" SelectionUnit="Cell"><DataGrid.CellStyle><Style TargetType="DataGridCell"><Setter Property="Background" Value="{Binding BackgroundColor,Converter={StaticResource dataSourceConvert}}"/></Style></DataGrid.CellStyle></DataGrid></Grid></Window>

Above is to define CellStyle of datagrid. And see converter definition below:

 public class DataSourceConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            switch ((string)value)
            {
                case "SMP":
                    return new SolidColorBrush(Colors.Pink);
                case "MID":
                    return new SolidColorBrush(Colors.Blue);
                case "INC":
                    return new SolidColorBrush(Colors.Orange);
                case "ACT":
                    return new SolidColorBrush(Colors.Green);
                case "DIS":
                    return new SolidColorBrush(Colors.Gray);
                case "RED":
                    return new SolidColorBrush(Colors.Yellow);
                default:
                    return new SolidColorBrush(Colors.White);
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

The background is binding with "backgroundColor" and it will be changed in Converter. The column is created by the following way:

void AddColumn(TimeUnit timeUnit)
        {
            string timeUnitName = "timeUnit" + newColumnIndex;
            //This is to save origin value of background
            //Converter will analyse this value and change it into Brush for DataGridCell Background
            string backgroundColor = "BackgroundColor";
            DataGridTextColumn dataGridTextColumnNew = new DataGridTextColumn() { Header = newColumnIndex.ToString(), Binding =new Binding( timeUnitName) };
            _dataGrid.Columns.Add(dataGridTextColumnNew);
            foreach (Step step in timeUnit.StepList)
            {
                string stepType;
                switch(step.StepType)
                {
                    case EStepType.ADDSAMPLESTEP:
                        stepType = "SMP";
                        break;
                    case EStepType.ADDMIDDLER1STEP:
                        stepType="MID";
                        break;
                    case EStepType.ADDACTIVATORR2STEP:
                        stepType="ACT";
                        break;
                    case EStepType.INCUBATIONSTEP:
                        stepType="INC";
                        break;
                    case EStepType.READTESTSTEP:
                        stepType="RED";
                        break;
                    case EStepType.DISPOSEWELL:
                        stepType="DIS";
                        break;
                    default:
                        stepType="NAN";
                        break;
                }
                foreach (dynamic item in items)
                {
                    if (step.Test.Patient.PatientCode == item.PatientCode)
                    {
                        item.TrySetMember(new SetPropertyBinder(timeUnitName), stepType);
                        //save the mark value in backgroundColor
                        item.TrySetMember(new SetPropertyBinder(backgroundColor), stepType);
                        break;
                    }
                }
            }
        }

At last Ifind all the cell is yellow. I follow the code and I find all the background of each cell is "RED" and the color is all yellow.

In fact only the backgroundColor value  of the last part cell of datarow is "RED". Not all.

Please tell me anything wrong in my program?


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>