Search code examples
c#wpfbindingradgridview

binding a property to the colomn in RadGridView in code behind


I have a list of custom classes that I have bound them to the RadGridView through the below code:

this.ItemsSource = CorrelationCalibraationGridInput.ListOfCalibratableCorrelationClasses;

then I have created the columns manually. For one of the columns that is check box column, I need to enable disable the check box binding to a property of class and set its check state based on another property of the class. I used the code below but the enablity does not bind to the IsNotCalibratedYet property. Can you explain why and how can I solve it?(note that the check state is correctly binded to the IsCalibratedUSed property of the class).

GridViewDataColumn IsCalibratedUSedColumn = new GridViewDataColumn()
{
    UniqueName = "IsCalibratedUSedColumn",
    Header = "Use calibrated",
    DataMemberBinding = new Binding("IsCalibratedUSed"),
    IsFilterable = false,
};
Binding enablityBinding = new Binding("IsNotCalibratedYet");
        enablityBinding.Mode= BindingMode.OneWay;
        enablityBinding.UpdateSourceTrigger= UpdateSourceTrigger.PropertyChanged;
        BindingOperations.SetBinding(IsCalibratedUSedColumn, GridViewDataColumn.IsEnabledProperty,enablityBinding );
        this.Columns.Add(IsCalibratedUSedColumn);

Solution

  • You should set the IsReadOnlyBinding property of the GridViewDataColumn to your Binding:

    GridViewDataColumn IsCalibratedUSedColumn = new GridViewDataColumn()
    {
        UniqueName = "IsCalibratedUSedColumn",
        Header = "Use calibrated",
        DataMemberBinding = new Binding("IsCalibratedUSed"),
        IsFilterable = false,
    };
    Binding enablityBinding = new Binding("IsNotCalibratedYet");
    enablityBinding.Mode = BindingMode.OneWay;
    enablityBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    
    IsCalibratedUSedColumn.IsReadOnlyBinding = enablityBinding;
    
    this.Columns.Add(IsCalibratedUSedColumn);
    

    Depending on whether your source property returns true/false you may want to use an InvertedBooleanConverter:

    Binding enablityBinding = new Binding("IsNotCalibratedYet");
    enablityBinding.Mode = BindingMode.OneWay;
    enablityBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    enablityBinding.Converter = new InvertedBooleanConverter();