我试图创建包含itemscontrol
的datagrid
的自定义标头,该标头绑定到ObservableCollection<DateTime>
。如何使用Databound自定义DataGrid的标题Itemscontrol
(基本上,我想创建12列,每个月一个 - >见图片)。
为什么这个代码不工作?
代码
<DataGrid x:Name="dgProjects" AutoGenerateColumns="False" ItemsSource="{Binding Projects}" AlternationCount="2" AlternatingRowBackground="LightGray">
<DataGrid.Columns>
...
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<ItemsControl Grid.Row="1" ItemsSource="{Binding Path=Monate}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Date}" Background="LightGreen" Margin="1"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
更新我将问题范围缩小到绑定。下面的代码工作FINDE
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<Grid>
<ItemsControl ItemsSource="{Binding Projects}">
<TextBlock Text="asdf" Background="LightGreen" Margin="1"/>
<TextBlock Text="asdf" Background="LightGreen" Margin="1"/>
<TextBlock Text="asdf" Background="LightGreen" Margin="1"/>
</ItemsControl>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
更新视图模型
public class vmProjektplanung : INotifyPropertyChanged
{
...
#region "Monate"
public event System.Collections.Specialized.NotifyCollectionChangedEventHandler OnMonateChanged;
private List<DateTime> monate;
public List<DateTime> Monate
{
get { return monate; }
set { monate = value; OnPropertyChanged(new PropertyChangedEventArgs("Monate")); }
}
private void Monate_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
OnMonateChanged?.Invoke(sender, e);
}
#endregion
...
}
您如何使用该模板? – Johannes
它位于DataGrid中。我只是更新了代码。还是你的意思是别的? –