在后面的代码中将 属性 绑定到 GridView 中的列

binding a property to the colomn in RadGridView in code behind

我有一个自定义 class 列表,我已通过以下代码将它们绑定到 RadGridView

this.ItemsSource = CorrelationCalibraationGridInput.ListOfCalibratableCorrelationClasses;

然后我手动创建了列。对于其中一列是复选框列,我需要启用禁用复选框绑定到 class 的 属性 并根据 属性 的另一个 属性 设置其检查状态20=]。 我使用了下面的代码,但启用没有绑定到 IsNotCalibratedYet 属性。你能解释一下为什么以及如何解决它吗?(注意检查状态正确绑定到 class 的 IsCalibratedUSed 属性)。

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);

您可以使用以下设置执行此操作:

public MainWindow()
{ 
    InitializeComponent();
    lv.ItemsSource = new Item[3] { new Item() { IsNotCalibratedYet=true }, new Item() { IsNotCalibratedYet = false }, new Item() { IsNotCalibratedYet = true } };
    gv.Columns.Add(new GridViewColumn()
    {
         DisplayMemberBinding = new Binding("IsNotCalibratedYet"),
    });
} 

其中:

public class Item
{
    public bool IsNotCalibratedYet { get; set; }
}

并且Xaml是

    <ListView Name="lv" > 
        <ListView.View> 
            <GridView x:Name="gv" AllowsColumnReorder="true"
                ColumnHeaderToolTip="Employee Information">  

            </GridView> 
        </ListView.View>
    </ListView>

您应该将 GridViewDataColumnIsReadOnlyBinding 属性 设置为您的 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);

根据你的来源 属性 returns true/false 你可能想要使用 InvertedBooleanConverter:

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