【问题标题】:Conditional cell editing in xamDataGridxamDataGrid 中的条件单元格编辑
【发布时间】: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 解决方案。

【问题讨论】:

  • 您能告诉我们实际传递了什么信息吗?并尝试使用您的绑定{Binding ..., TargetNullValue="Target Null"}{Binding ..., FallbackValue="Binding Failed"}。更多信息分别herehere。 HTH
  • 我正在尝试传递特定单元格的值(从 ViewModel 中的 DataTable 分配给它的值)。例如UI 中的Status 单元格应在第 2 行中禁用,因为它是 DbNull,但 StatusNew 仍将启用,因为它为空。
  • 您是否尝试过将绑定中的路径更改为“状态”而不是“自我”?
  • 是的,它没有给我所需的值。

标签: wpf xaml xamdatagrid


【解决方案1】:

首先,绑定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 &lt;Style TargetType="{x:Type igDP:CellValuePresenter}"&gt; 中可以找到的默认实现。

【讨论】:

  • 感谢您的回复。绑定似乎不起作用 - 除非我指定 Source 值,否则不会调用转换器。
  • @Maverick:什么是“源值”?
  • 现在我正在使用Source={RelativeSource Self}。我敢肯定,这是不正确的。但是,如果我没有为Source 传递任何值,调试器不会命中转换器。
  • @Maverick 你是把绑定放在 ControlTemplate 里面还是你还在使用FieldSettings?无论您如何设置绑定,后一个都不起作用。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 2014-09-24
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    相关资源
    最近更新 更多