ListView.ColumnHeader.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System.Windows.Data;
  2. namespace System.Windows.Forms
  3. {
  4. public class ColumnHeader
  5. {
  6. private static DataTemplate cellTemplate = (DataTemplate)System.Windows.Markup.XamlReader.Parse(
  7. "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>" +
  8. "<StackPanel Orientation=\"Horizontal\" Margin=\"2,1,2,1\">" +
  9. "<Image Source=\"{Binding ImageSource}\" Width=\"{Binding ImageSourceWidth}\" Height=\"{Binding ImageSourceHeight}\" Margin=\"0,0,4,0\"/>" +
  10. "<TextBlock x:Name=\"PART_TextBlock\" Text=\"{Binding SubItems[0].Text}\" VerticalAlignment=\"Center\"/>" +
  11. "</StackPanel></DataTemplate>");
  12. private ListView owner;
  13. internal System.Windows.Controls.GridViewColumn column { get; }
  14. public string Text
  15. {
  16. get => column.Header?.ToString();
  17. set => column.Header = value;
  18. }
  19. public int Width
  20. {
  21. get => (int)(column.Width * (owner != null ? owner.DpiScale : 1));
  22. set => column.Width = value / (owner != null ? owner.DpiScale : 1);
  23. }
  24. internal void SetOwner(ListView owner) => this.owner = owner;
  25. internal void SetBinding(int columnIndex)
  26. {
  27. if (columnIndex == 0)
  28. column.CellTemplate = cellTemplate;
  29. else
  30. column.DisplayMemberBinding = new Binding($"SubItems[{columnIndex}].Text");
  31. }
  32. public ColumnHeader()
  33. {
  34. column = new();
  35. }
  36. }
  37. }