wpf xamDataGrid 中的条件单元格编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22904242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conditional cell editing in xamDataGrid
提问by Maverick
I'm using a xamDataGrid. I want to disable the cell for STATUS column, if the value is DBNull. The issue seems to be with the FieldSettings, I'm not able to pass the correct value for that cell to the Converter. Here is the code :
我正在使用 xamDataGrid。如果值为 DBNull,我想禁用 STATUS 列的单元格。问题似乎与 FieldSettings 有关,我无法将该单元格的正确值传递给转换器。这是代码:
XAML :
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>
Edit :
编辑 :
The error is in this line :
错误在这一行:
<IgDp:FieldSettings AllowEdit="{Binding Source={RelativeSource Self}, Path=Self, Converter={StaticResource NullToBooleanConverter}}" />
ViewModel :
视图模型:
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");
}
}
Converter :
转换器:
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 : I'm looking for a pure ViewModel Solution.
IMP:我正在寻找纯 ViewModel 解决方案。
回答by floele
First of all, binding AllowEditwon't work because it applies to all cells of a column. You need a more local approach.
首先,绑定AllowEdit不起作用,因为它适用于列的所有单元格。您需要更本地化的方法。
Also, your binding is incorrect. It should read something like that:
另外,您的绑定不正确。它应该是这样的:
{Binding Path=DataItem[STATUS], Converter={StaticResource NullToBooleanConverter}}
There is a post at Infragisticswhere someone tries to achieve something similar. In a gist, what you need to do is setting a custom control template for the CellValuePresenterand add a trigger:
Infragistics上有一篇文章,有人试图实现类似的目标。总而言之,您需要做的是为 设置自定义控件模板CellValuePresenter并添加触发器:
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding DataItem[STATUS], Converter={StaticResource NullToBooleanConverter}}" Value="True">
<Setter Property="igEditors:ValueEditor.IsReadOnly" Value="false" />
</DataTrigger>
</ControlTemplate.Triggers>
Additionally, derive from XamDataGrid and add this piece of functionality to ensure IsReadOnly works as intended:
此外,从 XamDataGrid 派生并添加此功能以确保 IsReadOnly 按预期工作:
protected override void OnEditModeStarting(Infragistics.Windows.DataPresenter.Events.EditModeStartingEventArgs args)
{
var cell = args.Cell;
var cellEditor = Infragistics.Windows.DataPresenter.CellValuePresenter.FromCell(cell).Editor;
if (cellEditor != null && !cellEditor.IsReadOnly)
base.OnEditModeStarting(args);
else args.Cancel=true;
}
When setting a custom control template for CellValuePresenter, you need to need to include the default implementation you can find in %Program files%\Infragistics\NetAdvantage {version}\WPF\DefaultStyles\DataPresenter\DataPresenterGeneric_Express.xamlat <Style TargetType="{x:Type igDP:CellValuePresenter}">.
为 设置自定义控件模板时CellValuePresenter,您需要包含可以在%Program files%\Infragistics\NetAdvantage {version}\WPF\DefaultStyles\DataPresenter\DataPresenterGeneric_Express.xaml中找到的默认实现<Style TargetType="{x:Type igDP:CellValuePresenter}">。
回答by Martijn van Put
You're current code doesn't work due to an limitation of WPF, because the AllowEdit is not an Framework Element you cannot bind it the Data Context is only available to the visual tree. Take a look at this forum where they discuss this problem and give alternative solutions for workaround http://www.infragistics.com/community/forums/t/10907.aspx.
由于 WPF 的限制,您当前的代码不起作用,因为 AllowEdit 不是框架元素,您无法绑定它,数据上下文仅可用于可视化树。看看他们讨论这个问题的论坛,并提供替代解决方案http://www.infragistics.com/community/forums/t/10907.aspx。

