我试图实现一个自动生成其列的WPF DataGrid,并重用DataTemplates而不是创建相同的DataTemplates,除了它们的绑定。因此我找到了这个项目:
在尝试自己实现之后,我偶然发现了Datepicker控件的数据绑定错误。 Datepicker Control的所选日期属性应该绑定到Person类的Geburtsdatum属性。 CustomDataGridTemplateColumn类使用DataGrid中MainWindow.xaml中指定的绑定调整生成的DataGridCell的绑定。
现在没有绑定错误消息,但在使用datepicker控件选择日期时未设置该属性。其他自动生成的列不是CustomDataGridTemplateColumn类,因为我还没有为它们创建模板,工作得很好。 (调用各自属性的setter)。
1的 Person.cs
public DateTime Geburtsdatum
{
get
{
return _geburtsdatum;
}
set
{
if (_geburtsdatum.Equals(value))
return;
DateTime old = _geburtsdatum;
_geburtsdatum = value;
NotifyPropertyChanged<DateTime>("Geburtsdatum", old, _geburtsdatum);
}
}
2的 CustomDataGridTemplateColumn.cs
protected override FrameworkElement GenerateEditingElement(Microsoft.Windows.Controls.DataGridCell cell, object dataItem)
{
return this.LoadTemplateContent(true, dataItem, cell);
}
protected override FrameworkElement GenerateElement(Microsoft.Windows.Controls.DataGridCell cell, object dataItem)
{
return this.LoadTemplateContent(false, dataItem, cell);
}
private void ApplyBinding(DependencyObject target, DependencyProperty property)
{
BindingBase binding = this.Binding;
if (binding != null)
{
BindingOperations.SetBinding(target, property, binding);
}
else
{
BindingOperations.SetBinding(target, property, new Binding());
}
}
private FrameworkElement LoadTemplateContent(bool isEditing, object dataItem, Microsoft.Windows.Controls.DataGridCell cell)
{
DataTemplate template = this.ChooseCellTemplate(isEditing);
DataTemplateSelector templateSelector = this.ChooseCellTemplateSelector(isEditing);
if ((template == null) && (templateSelector == null))
{
return null;
}
ContentPresenter contentPresenter = new ContentPresenter();
this.ApplyBinding(contentPresenter, ContentPresenter.ContentProperty);
contentPresenter.ContentTemplate = template;
contentPresenter.ContentTemplateSelector = templateSelector;
return contentPresenter;
}
3的 MainWindow.xaml
<Window x:Class="PersonaldatenbankClient.View.MainWindow"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:PDCModel="clr-namespace:PersonaldatenbankClient.Model"
xmlns:PDCView="clr-namespace:PersonaldatenbankClient.View"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Title="MainWindow" Height="350" Width="525"
>
<Window.Resources>
<DataTemplate x:Key="DateCellTemplate">
<DatePicker
SelectedDate="{Binding Path=Content,
RelativeSource={RelativeSource TemplatedParent},
UpdateSourceTrigger=PropertyChanged,
Mode=TwoWay}"
diag:PresentationTraceSources.TraceLevel="High"/>
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<PDCModel:ViewModel></PDCModel:ViewModel>
</Window.DataContext>
<Grid>
<toolkit:DataGrid
ItemsSource="{Binding Values}"
CanUserAddRows="True"
CanUserDeleteRows="True"
CanUserReorderColumns="True"
CanUserResizeColumns="True"
CanUserResizeRows="True"
CanUserSortColumns="True"
AlternatingRowBackground="Gray"
AlternationCount="2"
SelectionMode="Extended"
SelectedValuePath="{Binding Path=SelectedValues}"
AutoGenerateColumns="True"
>
<PDCView:GenerateTemplateColumn.Columns>
<PDCView:AutoGenerateColumn
Column="Geburtsdatum"
CellTemplate="{StaticResource DateCellTemplate}"
CellEditingTemplate="{StaticResource DateCellTemplate}"
Binding="{Binding Path=Geburtsdatum}"/>
</PDCView:GenerateTemplateColumn.Columns>
</toolkit:DataGrid>
</Grid>
</Window>