This is a migrated thread and some comments may be shown as answers.

Binding Background of a cell to a Property

2 Answers 196 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Hans Smit
Top achievements
Rank 1
Hans Smit asked on 08 Apr 2010, 01:27 PM
Hi,

I'm trying to bind the background of a GridViewDataColumn to a property in my ViewModel:
<radGrid:GridViewDataColumn Header="State" Width="090" Background="{Binding Path=State, Converter={StaticResource StateToColorConverter}}" DataMemberBinding="{Binding Path=State}"/> 

Based on the value of the State property the background should change: if State = new, the background should be green, if State = rejected the value should be red, and so on.
However, if I bind the State property to the Background, i get a BindingExpression path error stating that my State property is not found on my object, while the same databinding works fine on the DataMemberBinding.
Anyone got a clue what is wrong here?

The Xaml:
<radGrid:RadGridView ItemsSource="{Binding Orders}" > 
     <radGrid:RadGridView.Columns> 
          <radGrid:GridViewDataColumn Header="State" Width="090" 
                                     
Background="{Binding Path=State, Converter={StaticResource StateToColorConverter}}" 
                                     
DataMemberBinding="{Binding Path=State}"/>  
     </radGrid:RadGridView.Columns> 
</radGrid:RadGridView> 

and the code from the viewModel:
private ObservableCollection<SingleOrder> _orders = new ObservableCollection<SingleOrder>()  
public ObservableCollection<SingleOrder> Orders { get { return _orders; } }  
 
public class SingleOrder   
        : ViewModelBase  
{  
        private Instrument _instrument;  
        public Instrument Instrument  
        {  
            get { return _instrument; }  
            set 
            {  
                if (!Instrument.Equals(_instrument, value))  
                {  
                    _instrument = value;  
                    this.OnPropertyChanged("Instrument");  
                }  
            }  
        }  
 
        private System.String _state;  
        public System.String State  
        {  
            get { return _state; }  
            set 
            {  
                if (!String.Equals(_state, value))  
                {  
                    _state = value;  
                    this.OnPropertyChanged("State");  
 
                }  
            }  
        }  
}  
 

Thanks a lot!

Hans

2 Answers, 1 is accepted

Sort by
0
Accepted
Tsvyatko
Telerik team
answered on 13 Apr 2010, 02:16 PM
Hello Hans Smit,

I have prepared project based on the code snippets sent. The point here is that one need to define the binding to the cell Background (via style), rather than binding the gridview column (as it will be bound only once). Please check it and let me know if you have any other question or items.


Regards,
Tsvyatko
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Hans Smit
Top achievements
Rank 1
answered on 14 Apr 2010, 12:25 PM
Yes!

That did the trick!
Thanks!

Hans
Tags
GridView
Asked by
Hans Smit
Top achievements
Rank 1
Answers by
Tsvyatko
Telerik team
Hans Smit
Top achievements
Rank 1
Share this question
or