[英]How to set a DataGrid column to DatePicker input programmatically
There are a few of articles explaining how to make a specific column in your DataGrid DatePickers , DatePickers , or DateTimePickers , but they all do it through XAML. 有几篇文章解释了如何在DataGrid DatePickers , DatePickers或DateTimePickers中创建特定的列,但是它们都是通过XAML来完成的。 Doing something like this:
做这样的事情:
<DataGrid AutoGenerateColumns="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Date}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Resulting in this: 结果是:
Is there a way to do in in C#? 有没有办法在C#中进行?
I input a DataTable from a SQL database and bind it to a DataGrid. 我从SQL数据库输入一个DataTable并将其绑定到DataGrid。 Like so:
像这样:
dataGrid.DataContext = dataTable.AsDataView();
// or maybe dataGrid.ItemsSource = dataTable.DefaultView;
// I'm not sure what the difference is
To my XAML that looks like this: 对于我的XAML,如下所示:
<DataGrid RowEditEnding="updateDatabase" ItemsSource="{Binding}" AutoGenerateColumns="True">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Visibility" Value="{Binding Visibility}" />
</Style>
</DataGrid.RowStyle>
</DataGrid>
I can go like this: 我可以这样:
dataTable.Rows[0][15] = new DateTime(1991, 9, 2);
or 要么
DatePicker dp = new DatePicker();
dp.SelectedDate = DateTime.Today;
dataTable.Rows[0][14] = dp;
But those just end up as date strings in the cell, not a DatePicker input type? 但是那些最终以单元格中的日期字符串结尾,而不是DatePicker输入类型? Is there some way to do this?
有什么办法可以做到这一点? For instance
例如
dataTable.Columns[13].DataType.Equals(typeof(DatePicker));
dataTable.Columns[13].DataType.Equals(typeof(DatePickerTextBox));
Thanks in advance. 提前致谢。
EDIT: 编辑:
It'd also be okay if I could do it partially through XAML. 如果我可以通过XAML部分完成它也可以。 For instance, if I make my XMAL:
例如,如果我制作XMAL:
<DataGrid RowEditEnding="updateDatabase" Name="taskTable" ItemsSource="{Binding}" AutoGenerateColumns="True" HorizontalAlignment="Left" Margin="25,50,25,25" VerticalAlignment="Top" Grid.IsSharedSizeScope="True" SelectionChanged="DataGrid_SelectionChanged_1">
<!-- VirtualizingStackPanel.IsVirtualizing="False" -->
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Visibility" Value="{Binding Visibility}" />
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTemplateColumn Header="dates">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding dates}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
The dates column from the the DataTable I load like so taskTable.DataContext = dataTable.DefaultView;
我从DataTable加载的dates列就像
taskTable.DataContext = dataTable.DefaultView;
Could I somehow get the dates columns to be the same one, with the data from the DataTable in C# under dates with the Template from XAML? 我能否以某种方式使date列与C#中的DataTable中的数据与XAML的Templates下的日期相同?
In this example, because the headers are the same, if I Select a Date in the left column, once I press enter it will go into the dates column on the right and be saved which is good. 在此示例中,因为标题相同,所以如果我在左列中选择一个日期,则按Enter键后它将进入右边的日期列中并保存,这很好。 I just want them to be the same column though.
我只是希望它们成为同一列。
Any ideas? 有任何想法吗?
There are plenty of solutions for if you don't have to autogenerate the columns, but I had to allow for columns to be dynamically added and removed, so there was no way around it. 如果您不必自动生成列,则有很多解决方案,但是我不得不允许动态添加和删除列,因此无法解决。
Here's what worked: 这是起作用的:
C# C#
Inside public partial class MainWindow: window
内部
public partial class MainWindow: window
public string [] dates = {"COLUMN NAMES", "THAT NEED", "TO BE DATES"};
public void addColumnTemplates(object sender, DataGridAutoGeneratingColumnEventArgs e){
string header = e.Column.Header.ToString();
if ( dates.Contains(header) )
{
MyDataGridTemplateColumn col = new MyDataGridTemplateColumn();
col.ColumnName = e.PropertyName;
col.CellTemplate = (DataTemplate)FindResource("datePickerTemplate");
e.Column = col;
e.Column.Header = e.PropertyName;
}
}
Outside public partial class MainWindow: window
外部
public partial class MainWindow: window
public class MyDataGridTemplateColumn : DataGridTemplateColumn
{
public string ColumnName
{
get;
set;
}
protected override System.Windows.FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
ContentPresenter cp = (ContentPresenter)base.GenerateElement(cell, dataItem);
BindingOperations.SetBinding(cp, ContentPresenter.ContentProperty, new Binding(this.ColumnName));
return cp;
}
}
public class MyData
{
public MyData(string name, bool data) { nameData = name; showData = data; }
public string nameData { get; set; }
public bool showData { get; set; }
}
XMAL XMAL
For datatemplate: 对于数据模板:
<Window.Resources>
<ResourceDictionary>
<DataTemplate x:Key="datePickerTemplate">
<DatePicker Text="{Binding}"
></DatePicker>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
And this is my XMAL for the datagrid 这是我的数据网格的XMAL
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="True"
AutoGeneratingColumn="addColumnTemplates">
</DataGrid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.