我試圖創建包含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中。我只是更新了代碼。還是你的意思是別的? –