由于无法将值粘贴到DataGridTemplateColumn中。我发现了一些有关创建自己的从DataGridBoundColumn派生的列类的建议。下面的示例在不使用模板的情况下将DatePicker添加到该列。

但是,此示例不允许我使用DatePicker手动设置值,我不确定为什么。我在想绑定有什么。它将加载我最初绑定到的日期值,因此它在其中途。

有趣的是,使用其他一些帮助程序类,我也能够粘贴日期,这是原始的目的。我只是不想破坏其他任何东西。 :-)

任何想法如何使datepicker选定值正确绑定?

class MyDateColumn : DataGridBoundColumn
{
    public string DateFormat { get; set; }
    protected override void CancelCellEdit(FrameworkElement editingElement, object uneditedValue)
    {
        DatePicker dp = editingElement as DatePicker;
        if (dp != null)
        {
            dp.SelectedDate = DateTime.Parse(uneditedValue.ToString());
        }
    }
    protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
    {
        DatePicker dp = new DatePicker();
        Binding b = new Binding();
        Binding bb = this.Binding as Binding;
        b.Path = bb.Path;
        b.Source = DatePicker.SelectedDateProperty;
        if (DateFormat != null)
        {
            DateTimeConverter dtc = new DateTimeConverter();
            b.Converter = dtc;
            b.ConverterParameter = DateFormat;
        }
        dp.SetBinding(DatePicker.SelectedDateProperty, this.Binding);

        return dp;
    }

    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        TextBlock txt = new TextBlock();
        Binding b = new Binding();
        Binding bb = this.Binding as Binding;
        b.Path = bb.Path;
        b.Source = cell.DataContext;

        if (DateFormat != null)
        {
            DateTimeConverter dtc = new DateTimeConverter();
            b.Converter = dtc;
            b.ConverterParameter = DateFormat;
        }
        txt.SetBinding(TextBlock.TextProperty, this.Binding);
        return txt;
    }

    protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
    {
        DatePicker dp = editingElement as DatePicker;
        if (dp != null)
        {
            DateTime? dt = dp.SelectedDate;
            if (dt.HasValue)
                return dt.Value;
        }
        return DateTime.Today;
    }

    protected override bool CommitCellEdit(FrameworkElement editingElement)
    {
        DatePicker dp = editingElement as DatePicker;
        dp.SelectedDate = DateTime.Today.AddYears(1);

        return true;
        //return base.CommitCellEdit(editingElement);
    }
}

最佳答案

解决方案是对CommitCellEdit()的修改。

    protected override bool CommitCellEdit(FrameworkElement editingElement)
    {
        DatePicker dp = editingElement as DatePicker;
        DateTime dt;
        try
        {
            dt = Convert.ToDateTime(dp.Text);
            dp.SelectedDate = dt;
        }
        catch (FormatException)
        {
            dp.Text = String.Empty;
        }


        BindingExpression binding = editingElement.GetBindingExpression(DatePicker.SelectedDateProperty);
        if (binding != null)
            binding.UpdateSource();
        return true;
        //return base.CommitCellEdit(editingElement);
    }


原始代码和帮助来自以下链接...

http://leeontech.wordpress.com/2009/01/21/creating-datagriddatecolumn-for-datagrid/#comment-1033

感谢您创建示例和帮助Lee!

关于c# - 如何在WPF中将值设置为自定义DataGridBound列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3611213/

10-16 04:20