Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
595
Binding converter doesn't work for unbound field
posted

Hi,

I am trying to set the background color for the cell at runtime which is not working.

        public ObservableCollection<Customer> Customers = new ObservableCollection<Customer>();
        private XamDataGrid xamDataGrid = new XamDataGrid();

        public MainWindow()
        {
            InitializeComponent();            
        }

        private void OnGetData(object sender, RoutedEventArgs e)
        {
            if (button1.IsEnabled == true)
            {
                Customer customer1 = new Customer { FirstName = "Julie", LastName = "Smith" };

                Customer customer2 = new Customer { FirstName = "Mark", LastName = "Smith" };

                Customers.Add(customer1);
                Customers.Add(customer2);

                this.xamDataGrid = new XamDataGrid
                {
                    FieldLayoutSettings = { AutoGenerateFields = false },
                    FieldSettings = { CellClickAction = CellClickAction.SelectCell },
                    DataSource = Customers,
                    GroupByAreaLocation = GroupByAreaLocation.None
                };

                this.xamDataGrid.FieldLayoutInitialized += this.GridOnFieldLayoutInitialized;
                this.grid1.Children.Clear();
                this.grid1.Children.Add(this.xamDataGrid);

               button1.IsEnabled = false;
            }            
        }

        private void GridOnFieldLayoutInitialized(object sender, Infragistics.Windows.DataPresenter.Events.FieldLayoutInitializedEventArgs e)
        {
            UnboundField unboundField1 = new UnboundField
            {
                Name = "FirstName",
                BindingMode = BindingMode.TwoWay,
                BindingPath = new PropertyPath("FirstName")
            };

            Style style1 = new Style { TargetType = typeof(CellValuePresenter) };
            System.Windows.Data.Binding binding1 = new System.Windows.Data.Binding
            {
                Path = new PropertyPath("FirstName"),
                Converter = new BackGroundConverter()
            };
            Setter setter1 = new Setter(BackgroundProperty, binding1);
            style1.Setters.Add(setter1);
            unboundField1.Settings.CellValuePresenterStyle = style1;
            e.FieldLayout.Fields.Add(unboundField1);

            UnboundField unboundField = new UnboundField
            {
                Name = "LastName",
                BindingMode = BindingMode.TwoWay,
                BindingPath = new PropertyPath("LastName")
            };

            Style style = new Style { TargetType = typeof(CellValuePresenter) };
            Setter setter = new Setter(BackgroundProperty, new SolidColorBrush(Colors.RoyalBlue));
            style.Setters.Add(setter);
            unboundField.Settings.CellValuePresenterStyle = style;
            e.FieldLayout.Fields.Add(unboundField);
        }

I am able to set the background for LastName column but not for the FirstName. Could you please point out what is wrong?