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?

Parents
  • 138253
    Verified Answer
    Offline posted

    Hello Ramesh,

    Thank you for your post. I have been looking into your question and I can say that the DataContext of the Binding you use in the Setter for the Background of the first UnboundField is DataRecord, so you have to change the BindingPath from "FirstName" to "DataItem.FirstName". This is for the binding with the converter. Please let me know if this helps you or you have further questions on this matter.

    Looking forward for your reply.

Reply Children
No Data