【发布时间】:2014-04-07 05:28:32
【问题描述】:
我正在使用 xamDataGrid。如果值为 DBNull,我想禁用 STATUS 列的单元格。问题似乎出在 FieldSettings,我无法将该单元格的正确值传递给转换器。这是代码:
XAML:
<Window.Resources>
<dbNullConverter:DBNullToBooleanConverter x:Key="NullToBooleanConverter" />
</Window.Resources>
<Grid>
<DockPanel>
<IgDp:XamDataGrid x:Name="gridData" DataSource="{Binding Path=TempDataTable}">
<IgDp:XamDataGrid.FieldLayoutSettings>
<IgDp:FieldLayoutSettings AutoGenerateFields="True"/>
</IgDp:XamDataGrid.FieldLayoutSettings>
<IgDp:XamDataGrid.FieldSettings>
<IgDp:FieldSettings AllowEdit="True" />
</IgDp:XamDataGrid.FieldSettings>
<IgDp:XamDataGrid.FieldLayouts>
<IgDp:FieldLayout>
<IgDp:Field Name="STATUS" Label="STATUS">
<IgDp:Field.Settings>
<IgDp:FieldSettings AllowEdit="{Binding Source={RelativeSource Self}, Path=Self, Converter={StaticResource NullToBooleanConverter}}" />
</IgDp:Field.Settings>
</IgDp:Field>
<IgDp:Field Name="ROWID" />
<IgDp:Field Name="RESULT" Label="VALUE" />
<IgDp:Field Name="HasRowBeenEdited" Label="Edited ?">
<IgDp:Field.Settings>
<IgDp:FieldSettings EditorType="{x:Type igEditors:XamCheckEditor}"/>
</IgDp:Field.Settings>
</IgDp:Field>
</IgDp:FieldLayout>
</IgDp:XamDataGrid.FieldLayouts>
</IgDp:XamDataGrid>
</DockPanel>
</Grid>
编辑:
错误在这一行:
<IgDp:FieldSettings AllowEdit="{Binding Source={RelativeSource Self}, Path=Self, Converter={StaticResource NullToBooleanConverter}}" />
视图模型:
public class DBNullConverterViewModel : INotifyPropertyChanged
{
private DataTable tempDataTable;
public DataTable TempDataTable
{
get { return tempDataTable; }
set
{
tempDataTable = value;
RaisedPropertyChanged("tempDataTable");
}
}
public DBNullConverterViewModel()
{
TempDataTable = new DataTable();
GetValue();
}
private void GetValue()
{
tempDataTable.Columns.Add(new DataColumn("ROWID", typeof(Int32)));
tempDataTable.Columns.Add(new DataColumn("STATUS", typeof(string)));
tempDataTable.Columns.Add(new DataColumn("StatusNew", typeof(string)));
tempDataTable.Columns.Add(new DataColumn("HasRowBeenEdited", typeof(bool)));
DataRow row = tempDataTable.NewRow();
row["ROWID"] = 1;
row["STATUS"] = "Active";
row["StatusNew"] = "New";
row["HasRowBeenEdited"] = true;
tempDataTable.Rows.Add(row);
tempDataTable.AcceptChanges();
DataRow row1 = tempDataTable.NewRow();
row1["ROWID"] = 2;
row1["STATUS"] = DBNull.Value;
row1["StatusNew"] = null;
row1["HasRowBeenEdited"] = DBNull.Value;
tempDataTable.Rows.Add(row1);
tempDataTable.AcceptChanges();
RaisedPropertyChanged("tempDataTable");
}
}
转换器:
public class DBNullToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == DBNull.Value)
return false;
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
IMP :我正在寻找纯 ViewModel 解决方案。
【问题讨论】:
-
我正在尝试传递特定单元格的值(从 ViewModel 中的 DataTable 分配给它的值)。例如UI 中的
Status单元格应在第 2 行中禁用,因为它是 DbNull,但StatusNew仍将启用,因为它为空。 -
您是否尝试过将绑定中的路径更改为“状态”而不是“自我”?
-
是的,它没有给我所需的值。
标签: wpf xaml xamdatagrid