Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Scale items with ViewBox within an ItemsControl

Scale items with ViewBox within an ItemsControl

Scheduled Pinned Locked Moved WPF
helpwpftutorialquestion
2 Posts 1 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kenneth Haugland
    wrote on last edited by
    #1

    So I have some FrameworkElements that I want to scale with a ViewBox. This works fine with normal XAML code:

    But as soon as I want to join them together and scale them appropriately to the available size I have a problem:

    The ViewBox simply refuses to scale them according to the set size of the ViewBox. Any ideas on how to fix this?

    K 1 Reply Last reply
    0
    • K Kenneth Haugland

      So I have some FrameworkElements that I want to scale with a ViewBox. This works fine with normal XAML code:

      But as soon as I want to join them together and scale them appropriately to the available size I have a problem:

      The ViewBox simply refuses to scale them according to the set size of the ViewBox. Any ideas on how to fix this?

      K Offline
      K Offline
      Kenneth Haugland
      wrote on last edited by
      #2

      Still havent found out what was wrong with this, but here is one of the FrameworkElements Im trying to bind the ItemsControl to

      public class SourceDiagramBase : FrameworkElement, INotifyPropertyChanged
      {
          private VisualCollection \_children;
      
          public SourceDiagramBase()
          {
              \_children = new VisualCollection(this);
              \_children.Add(CreateDrawingVisual());
          }
      
          protected override int VisualChildrenCount { get { return \_children.Count; } }
      
      
          protected override Visual GetVisualChild(int index)
          {
              return \_children\[index\];
          }
      
      
      
          public double Scale
          {
              get { return (double)GetValue(ScaleProperty); }
              set { SetValue(ScaleProperty, value); }
          }
      
          // Using a DependencyProperty as the backing store for Scale.  This enables animation, styling, binding, etc...
          public static readonly DependencyProperty ScaleProperty =
              DependencyProperty.Register("Scale", typeof(double), typeof(SourceDiagramBase), new PropertyMetadata(20d));
      
          protected override Size MeasureOverride(Size availableSize)
          {
              return new Size(this.Scale \* 12d, this.Scale \* 12d);
          }
      
      
          private DrawingVisual CreateDrawingVisual()
          {
              DrawingVisual drawingVisual = new DrawingVisual();
              DrawingContext drawingContext = drawingVisual.RenderOpen();
      
              drawingContext.PushTransform(new ScaleTransform(this.Scale, this.Scale));
      
              double thick = 0.04;
              drawingContext.DrawEllipse(Brushes.Transparent, new Pen(Brushes.Black, thick), new Point(6, 6), 3,3);
      
              drawingContext.DrawLine(new Pen(Brushes.Black, thick), new Point(6, 3), new Point(6, 2));
              drawingContext.DrawLine(new Pen(Brushes.Black, thick), new Point(6, 9), new Point(6, 10));
      
              drawingContext.DrawLine(new Pen(Brushes.Black, thick), new Point(6, 2), new Point(12, 2));
              drawingContext.DrawLine(new Pen(Brushes.Black, thick), new Point(6, 10), new Point(12, 10));
      
              drawingContext.Close();
              return drawingVisual;
          }
      
          public event PropertyChangedEventHandler PropertyChanged;
      
          // This method is called by the Set accessor of each property.
          // The CallerMemberName attribute that is applied to the optional propertyName
          // parameter causes the property name of the caller to be substituted as
      
      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups