【发布时间】:2013-04-10 10:11:47
【问题描述】:
我正在尝试将自定义数据对象列表绑定到数据网格并实现以下行为。
- 填充网格
- 根据对象数据禁用某些单元格。
考虑以下 DataGrid
<DataGrid ItemsSource="{Binding Path=CustomObjectList}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=FieldName}"
Header="Field Name"
IsReadOnly="True" />
<DataGridCheckBoxColumn Binding="{Binding Path=Compare}"
Header="Compare" />
<DataGridTextColumn Binding="{Binding Path=Tolerance}"
Header="Tolerance" />
</DataGrid.Columns>
</DataGrid>
有了这样的支持对象...
public class CustomObject: BaseModel
{
private bool _compare;
private bool _disableTolerance;
private string _fieldName;
private bool _mustCompare;
private double _tolerance;
/// <summary>
/// Gets or sets the compare.
/// </summary>
/// <value>The compare.</value>
public bool Compare
{
get
{
return this._compare;
}
set
{
this._compare = value;
this.NotifyPropertyChange("Compare");
}
}
/// <summary>
/// Gets or sets the disable tolerance.
/// </summary>
/// <value>The disable tolerance.</value>
public bool DisableTolerance
{
get
{
return this._disableTolerance;
}
set
{
this._disableTolerance = value;
this.NotifyPropertyChange("DisableTolerance");
}
}
/// <summary>
/// Gets or sets the name of the field.
/// </summary>
/// <value>The name of the field.</value>
public string FieldName
{
get
{
return this._fieldName;
}
set
{
this._fieldName = value;
this.NotifyPropertyChange("FieldName");
}
}
/// <summary>
/// Gets or sets the must compare.
/// </summary>
/// <value>The must compare.</value>
public bool MustCompare
{
get
{
return this._mustCompare;
}
set
{
this._mustCompare = value;
this.NotifyPropertyChange("MustCompare");
}
}
/// <summary>
/// Gets or sets the tolerance.
/// </summary>
/// <value>The tolerance.</value>
public double Tolerance
{
get
{
return this._tolerance;
}
set
{
this._tolerance = value;
this.NotifyPropertyChange("Tolerance");
}
}
}
您可以考虑这样填充 CustomObjectList...
this.ComparisonsAndTolerances.Add(new ComparisonSettingsTolerances()
{
FieldName = "Alpha",
Compare = true,
MustCompare = true,
Tolerance = 0,
DisableTolerance = false
});
this.ComparisonsAndTolerances.Add(new ComparisonSettingsTolerances()
{
FieldName = "Bravo",
Compare = true,
MustCompare = false,
Tolerance = 0,
DisableTolerance = true
});
因此,FieldName、Compare 和 Tolerance 属性当然会适当地填充到网格中。
但是,我想要实现的是,当 MustCompare 为真时,该单元格被标记为只读。当 DisableTolerance 为 true 时,该单元格被标记为只读。
显然,这会因 4 种不同组合而因单元格和行而异,但我希望通过绑定来实现。
我试过了
IsReadOnly="{Binding Path=MustCompare}"
和
IsReadOnly="{Binding Path=CustomObjectList/MustCompare}"
但这些都不起作用。
谢谢。
【问题讨论】:
标签: c# wpf xaml data-binding datagrid