14
<DataGrid Name="myfirstdg"
          Grid.Row="2"
          AutoGenerateColumns="False"
          CanUserSortColumns="False"
          CanUserAddRows="False"
          CanUserDeleteRows="False"
          SelectionUnit="Cell">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Date"
                            Binding="{Binding Path=date}"
                            Width="SizeToCells"
                            IsReadOnly="True"
                            MinWidth="100"/>
    </DataGrid.Columns>
</DataGrid>

我有一个简单的,里面DataGrid有一个DataGridTextColumn。如何添加一个Datepicker到我的DataGridTextColumn

4

3 回答 3

33

正如 Nitesh 所说,使用DataGridTemplateColumn

<DataGridTemplateColumn Header="Pick a Date">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding myDate}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <DatePicker SelectedDate="{Binding myDate}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
于 2013-09-08T05:12:52.743 回答
5

我在数据网格的每一列中都放置了一个 DatePicker,这是我DataGrid在 windows 构造函数中分配给的辅助方法。此方法还可以取消在 DataGrid 中显示效果不佳的复杂对象的生成。

随心所欲地适应!

public MainWindow()
{
    InitializeComponent();
    myDataGrid.AutoGeneratingColumn += DataGridUtilities.dataGrid_AutoGeneratingColumn;
}

public static class DataGridUtilities
{
    public static void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        if (!IsTypeOrNullableOfType(e.PropertyType, typeof(string))
            && !IsNullableOfValueType(e.PropertyType))
        {
            e.Cancel = true;
        }
        else if (IsTypeOrNullableOfType(e.PropertyType, typeof (DateTime)))
        {
            DataGridTemplateColumn col = new DataGridTemplateColumn();
            col.Header = e.Column.Header;
            FrameworkElementFactory datePickerFactoryElem = new FrameworkElementFactory(typeof (DatePicker));
            Binding dateBind= new Binding(e.PropertyName);
            dateBind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            dateBind.Mode = BindingMode.TwoWay;
            datePickerFactoryElem.SetValue(DatePicker.SelectedDateProperty, dateBind);
            datePickerFactoryElem.SetValue(DatePicker.DisplayDateProperty, dateBind);
            DataTemplate cellTemplate = new DataTemplate();
            cellTemplate.VisualTree = datePickerFactoryElem;
            col.CellTemplate = cellTemplate;
            e.Column = col;//Set the new generated column
        }
    }


    private static bool IsTypeOrNullableOfType(Type propertyType, Type desiredType)
    {
        return (propertyType == desiredType || Nullable.GetUnderlyingType(propertyType) == desiredType);
    }

    private static bool IsNullableOfValueType(Type propertyType)
    {
        return (propertyType.IsValueType ||
                (Nullable.GetUnderlyingType(propertyType) != null &&
                 Nullable.GetUnderlyingType(propertyType).IsValueType));
    }
}
于 2015-04-28T16:13:13.513 回答
2

这建立在@Guish 的答案之上,但将其封装到一个新的列类中。

private void Grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyType == typeof(DateTime))
    {
        e.Column = new DataGridDateTimeColumn((DataGridBoundColumn)e.Column);
    }
}

internal class DataGridDateTimeColumn : DataGridBoundColumn
{
    public DataGridDateTimeColumn(DataGridBoundColumn column)
    {
        Header = column.Header;
        Binding = (Binding)column.Binding;
    }

    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        var control = new TextBlock();
        BindingOperations.SetBinding(control, TextBlock.TextProperty, Binding);
        return control;
    }

    protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
    {
        var control = new DatePicker();
        control.PreviewKeyDown += Control_PreviewKeyDown;
        BindingOperations.SetBinding(control, DatePicker.SelectedDateProperty, Binding);
        BindingOperations.SetBinding(control, DatePicker.DisplayDateProperty, Binding);
        return control;
    }

    private void Control_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (e.Key == System.Windows.Input.Key.Return)
        {
            DataGridOwner.CommitEdit();
        }
    }
}
于 2018-01-31T08:11:24.720 回答