xamDataGrid中的条件单元格编辑

时间:2014-04-07 05:28:33

标签: wpf xaml xamdatagrid

我正在使用xamDataGrid。如果值为DBNull,我想禁用STATUS列的单元格。问题似乎与FieldSettings有关,我无法将该单元格的正确值传递给Converter。这是代码:

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}}" />

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

    }
}

转换器:

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解决方案。

2 个答案:

答案 0 :(得分:2)

首先,绑定AllowEdit不起作用,因为它适用于列的所有单元格。你需要一种更本地化的方法。

此外,您的绑定不正确。它应该是这样的:

{Binding Path=DataItem[STATUS], Converter={StaticResource NullToBooleanConverter}}

There is a post at Infragistics有人试图获得类似的东西。在要点中,您需要做的是为CellValuePresenter设置自定义控件模板并添加触发器:

<ControlTemplate.Triggers>
    <DataTrigger Binding="{Binding DataItem[STATUS], Converter={StaticResource NullToBooleanConverter}}" Value="True">
      <Setter Property="igEditors:ValueEditor.IsReadOnly" Value="false" />
    </DataTrigger>
  </ControlTemplate.Triggers>

此外,派生自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;
}

CellValuePresenter设置自定义控件模板时,您需要在%Program files%\Infragistics\NetAdvantage {version}\WPF\DefaultStyles\DataPresenter\DataPresenterGeneric_Express.xaml <Style TargetType="{x:Type igDP:CellValuePresenter}">包含{{1}}中可以找到的默认实现。

答案 1 :(得分:0)

由于WPF的限制,您当前的代码不起作用,因为AllowEdit不是您无法绑定它的框架元素,数据上下文仅可用于可视树。看一下他们讨论这个问题的论坛,并为解决方法http://www.infragistics.com/community/forums/t/10907.aspx提供替代解决方案。