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. WPF: Can't get what I need to do to get things right (responsive controls).

WPF: Can't get what I need to do to get things right (responsive controls).

Scheduled Pinned Locked Moved WPF
questioncsharpmobilewpfhelp
14 Posts 3 Posters 0 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.
  • P Pew_new

    hello guys, there's an issue as follows: When I change resolution of the screen, my controls so to speak - "swim". You can see it if you'll start my simpliest test apps (a code is provided). A question: How to get my controls to be responsive to resolution alteration?

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #2

    Avoid layout out controls using margins, and avoid specifying a width and height for controls that don't need them. For basic layout, use one of the standard layout panels, and use the horizontal and vertical layout properties to position your controls. If you're creating a drawing, it's usually better to use the Canvas, and set the Canvas.Left and Canvas.Top positions instead. If you want the drawing to resize with the container, you'll probably want a Viewbox as well. Panels Overview | Microsoft Docs[^] Canvas Class (System.Windows.Controls) | Microsoft Docs[^] Viewbox | Microsoft Docs[^] Eg:

    <Grid ShowGridLines="True">
    <Grid.RowDefinitions>
    <RowDefinition Height="80"/>
    <RowDefinition Height="1*"/>
    <RowDefinition Height="160"/>
    </Grid.RowDefinitions>

    <Canvas Grid.Row="1" HorizontalAlignment="Center" Width="1024" ClipToBounds="True">
        <!-- NB: No "Grid.Row" on the children; Margin replaced with Canvas.Left and Canvas.Top: -->
        <Path Data="M319.5,255.83333 L511.5,255.47633" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="1.357" Canvas.Left="255.468" Canvas.Top="175.5" Stretch="Fill" Stroke="Black" VerticalAlignment="Top" Width="193"/>
        ...
        <TextBlock x:Name="textBlock2" HorizontalAlignment="Left" Canvas.Left="387.968" Canvas.Top="10" Grid.Row="1" TextWrapping="Wrap" Text="Center vert. line of the 1024x768 screen" VerticalAlignment="Top"/>
    </Canvas>
    
    <!-- NB: No Margin, Width or Height; use horizontal and vertical alignment instead: -->
    <TextBlock x:Name="textBlock1" HorizontalAlignment="Center" TextWrapping="Wrap" Text="I need that Row" VerticalAlignment="Center" FontSize="24" TextAlignment="Center"/>
    <TextBlock x:Name="te
    

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    P 2 Replies Last reply
    0
    • Richard DeemingR Richard Deeming

      Avoid layout out controls using margins, and avoid specifying a width and height for controls that don't need them. For basic layout, use one of the standard layout panels, and use the horizontal and vertical layout properties to position your controls. If you're creating a drawing, it's usually better to use the Canvas, and set the Canvas.Left and Canvas.Top positions instead. If you want the drawing to resize with the container, you'll probably want a Viewbox as well. Panels Overview | Microsoft Docs[^] Canvas Class (System.Windows.Controls) | Microsoft Docs[^] Viewbox | Microsoft Docs[^] Eg:

      <Grid ShowGridLines="True">
      <Grid.RowDefinitions>
      <RowDefinition Height="80"/>
      <RowDefinition Height="1*"/>
      <RowDefinition Height="160"/>
      </Grid.RowDefinitions>

      <Canvas Grid.Row="1" HorizontalAlignment="Center" Width="1024" ClipToBounds="True">
          <!-- NB: No "Grid.Row" on the children; Margin replaced with Canvas.Left and Canvas.Top: -->
          <Path Data="M319.5,255.83333 L511.5,255.47633" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="1.357" Canvas.Left="255.468" Canvas.Top="175.5" Stretch="Fill" Stroke="Black" VerticalAlignment="Top" Width="193"/>
          ...
          <TextBlock x:Name="textBlock2" HorizontalAlignment="Left" Canvas.Left="387.968" Canvas.Top="10" Grid.Row="1" TextWrapping="Wrap" Text="Center vert. line of the 1024x768 screen" VerticalAlignment="Top"/>
      </Canvas>
      
      <!-- NB: No Margin, Width or Height; use horizontal and vertical alignment instead: -->
      <TextBlock x:Name="textBlock1" HorizontalAlignment="Center" TextWrapping="Wrap" Text="I need that Row" VerticalAlignment="Center" FontSize="24" TextAlignment="Center"/>
      <TextBlock x:Name="te
      
      P Offline
      P Offline
      Pew_new
      wrote on last edited by
      #3

      Hello Richard, first of all thank you for your input. I've tried to test first sample and faced with the following warning related to Canvas: "The specified value cannot be assigned to the collection. The following type was expected: UIElement" I made a screenshot, however, it seems, it isn't possible posting of images here. So I have a question. What's wrong with Canvas in that case? :zzz:

      1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Avoid layout out controls using margins, and avoid specifying a width and height for controls that don't need them. For basic layout, use one of the standard layout panels, and use the horizontal and vertical layout properties to position your controls. If you're creating a drawing, it's usually better to use the Canvas, and set the Canvas.Left and Canvas.Top positions instead. If you want the drawing to resize with the container, you'll probably want a Viewbox as well. Panels Overview | Microsoft Docs[^] Canvas Class (System.Windows.Controls) | Microsoft Docs[^] Viewbox | Microsoft Docs[^] Eg:

        <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
        <RowDefinition Height="80"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="160"/>
        </Grid.RowDefinitions>

        <Canvas Grid.Row="1" HorizontalAlignment="Center" Width="1024" ClipToBounds="True">
            <!-- NB: No "Grid.Row" on the children; Margin replaced with Canvas.Left and Canvas.Top: -->
            <Path Data="M319.5,255.83333 L511.5,255.47633" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="1.357" Canvas.Left="255.468" Canvas.Top="175.5" Stretch="Fill" Stroke="Black" VerticalAlignment="Top" Width="193"/>
            ...
            <TextBlock x:Name="textBlock2" HorizontalAlignment="Left" Canvas.Left="387.968" Canvas.Top="10" Grid.Row="1" TextWrapping="Wrap" Text="Center vert. line of the 1024x768 screen" VerticalAlignment="Top"/>
        </Canvas>
        
        <!-- NB: No Margin, Width or Height; use horizontal and vertical alignment instead: -->
        <TextBlock x:Name="textBlock1" HorizontalAlignment="Center" TextWrapping="Wrap" Text="I need that Row" VerticalAlignment="Center" FontSize="24" TextAlignment="Center"/>
        <TextBlock x:Name="te
        
        P Offline
        P Offline
        Pew_new
        wrote on last edited by
        #4

        Hello Richard, first of all thank you for replying. Currently doing comprehensive test of the first variant (with Canvas). Please share with experience; how to move eg. my sketch, if it's situated within Canvas space. I asked because I'd failed to move the sketch separately (it moves with Canvas itself only).:confused: Thanks in advance! P.S. the first feeling. It seems Canvas' variant strongly keeps original (1024x768) position of the controls. With no adaptation/resizing, though. In other words the controls don't "swim", at least. :thumbsup: To be continued. Yes, it seems that's it! The ViewBox's variant, in contrast with Canvas' variant, strongly keeps position of the controls and scales them correspondingly to given aspect ratio/resolution.:thumbsup: To be honest, I tried to use symbiosis of Canvas and ViewBox; true, with no success. Because I didn't use some significant (as it turned out) parameters which you offered. P.P.S. Previously, I asked the same question at many places. I got a strong sense that nobody had got me. What I really need. I provided the same sample code, though. As though the people never faced with such kind of issues. Perhaps they don't port their apps to another workstations or make "Calculator" apps only, or so. :confused: my regards.

        Richard DeemingR 1 Reply Last reply
        0
        • P Pew_new

          Hello Richard, first of all thank you for replying. Currently doing comprehensive test of the first variant (with Canvas). Please share with experience; how to move eg. my sketch, if it's situated within Canvas space. I asked because I'd failed to move the sketch separately (it moves with Canvas itself only).:confused: Thanks in advance! P.S. the first feeling. It seems Canvas' variant strongly keeps original (1024x768) position of the controls. With no adaptation/resizing, though. In other words the controls don't "swim", at least. :thumbsup: To be continued. Yes, it seems that's it! The ViewBox's variant, in contrast with Canvas' variant, strongly keeps position of the controls and scales them correspondingly to given aspect ratio/resolution.:thumbsup: To be honest, I tried to use symbiosis of Canvas and ViewBox; true, with no success. Because I didn't use some significant (as it turned out) parameters which you offered. P.P.S. Previously, I asked the same question at many places. I got a strong sense that nobody had got me. What I really need. I provided the same sample code, though. As though the people never faced with such kind of issues. Perhaps they don't port their apps to another workstations or make "Calculator" apps only, or so. :confused: my regards.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #5

          To move things around on the canvas, you'll need to update the Canvas.Top and Canvas.Left properties on the things you want to move. If you want to move a group of shapes as a single unit, the simplest option would probably be to use nested Canvas panels. You can then change the Canvas.Left and Canvas.Top properties on the child canvas to move the entire group.

          <Canvas Grid.Row="1">
          <!-- Bus: -->
          <Canvas Canvas.Top="..." Canvas.Left="...">
          <Path ... />
          ...
          </Canvas>

          <!-- Sign: -->
          <Canvas Canvas.Top="..." Canvas.Left="...">
              <Path ... />
              ...
          </Canvas>
          
          ...
          

          </Canvas>

          You'll obviously need to adjust the position of the shapes within the nested canvas, since they'll now be relative to the position of the nested canvas rather than the parent canvas. So if your shape was previously positioned at (100, 50), and is now in a nested canvas positioned at (90, 50), the shape would need to be at (10, 0). WPF also has strong support for animation: Animation Overview | Microsoft Docs[^] Animation using Storyboards in WPF[^] Advanced Animations in WPF[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          P 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            To move things around on the canvas, you'll need to update the Canvas.Top and Canvas.Left properties on the things you want to move. If you want to move a group of shapes as a single unit, the simplest option would probably be to use nested Canvas panels. You can then change the Canvas.Left and Canvas.Top properties on the child canvas to move the entire group.

            <Canvas Grid.Row="1">
            <!-- Bus: -->
            <Canvas Canvas.Top="..." Canvas.Left="...">
            <Path ... />
            ...
            </Canvas>

            <!-- Sign: -->
            <Canvas Canvas.Top="..." Canvas.Left="...">
                <Path ... />
                ...
            </Canvas>
            
            ...
            

            </Canvas>

            You'll obviously need to adjust the position of the shapes within the nested canvas, since they'll now be relative to the position of the nested canvas rather than the parent canvas. So if your shape was previously positioned at (100, 50), and is now in a nested canvas positioned at (90, 50), the shape would need to be at (10, 0). WPF also has strong support for animation: Animation Overview | Microsoft Docs[^] Animation using Storyboards in WPF[^] Advanced Animations in WPF[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            P Offline
            P Offline
            Pew_new
            wrote on last edited by
            #6

            hello Richard, While playing around "Symbiosis" of ViewBox and Canvas I stumbled across a weird behavior of Canvas. Eg., both ViewBox and Canvas are (Grid.Row="1"). I've got "Row1" dimensions and made a Canvas (Sketch inside) with exactly same dimensions. Then I put it into a ViewBox. Yes, as I wrote yesterday this "Symbiosis" keeps position and scales controls. That's true. However, I noticed one weirdness. I colored Canvas and when I maximized window (1920x1080) I had seen that Canvas hadn't overlapped entire width of the window. There were two pretty wide, vertical non-colored "columns", to the left and right of colored Canvas. P.S. I've tried to play with Canvas' height and width, to no avail. It just disappears, if it has no dimensions. Code:

            Richard DeemingR 1 Reply Last reply
            0
            • P Pew_new

              hello Richard, While playing around "Symbiosis" of ViewBox and Canvas I stumbled across a weird behavior of Canvas. Eg., both ViewBox and Canvas are (Grid.Row="1"). I've got "Row1" dimensions and made a Canvas (Sketch inside) with exactly same dimensions. Then I put it into a ViewBox. Yes, as I wrote yesterday this "Symbiosis" keeps position and scales controls. That's true. However, I noticed one weirdness. I colored Canvas and when I maximized window (1920x1080) I had seen that Canvas hadn't overlapped entire width of the window. There were two pretty wide, vertical non-colored "columns", to the left and right of colored Canvas. P.S. I've tried to play with Canvas' height and width, to no avail. It just disappears, if it has no dimensions. Code:

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #7

              The Viewbox has Stretch="Uniform". That means it won't change the aspect ratio of the child Canvas. If you resize the window so that row 1 has an aspect ratio that isn't 256 × 125 (1024 × 500), then you'll end up with spaces around your Viewbox, either on the left and right sides, or on the top and bottom sides. If you want to fill the entire background of the row, add a Rectangle to the same grid row, outside of the Viewbox, and set your fill there. You'll need to add it to the markup before the Viewbox, so that the rectangle appears behind it.

              <Rectangle Grid.Row="1" Fill="Bisque" />

              <Viewbox Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform" StretchDirection="Both">
              <Canvas Width="1024" Height="500"> <!-- No background colour here -->
              ...
              </Canvas>
              </Viewbox>


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              P 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                The Viewbox has Stretch="Uniform". That means it won't change the aspect ratio of the child Canvas. If you resize the window so that row 1 has an aspect ratio that isn't 256 × 125 (1024 × 500), then you'll end up with spaces around your Viewbox, either on the left and right sides, or on the top and bottom sides. If you want to fill the entire background of the row, add a Rectangle to the same grid row, outside of the Viewbox, and set your fill there. You'll need to add it to the markup before the Viewbox, so that the rectangle appears behind it.

                <Rectangle Grid.Row="1" Fill="Bisque" />

                <Viewbox Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform" StretchDirection="Both">
                <Canvas Width="1024" Height="500"> <!-- No background colour here -->
                ...
                </Canvas>
                </Viewbox>


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                P Offline
                P Offline
                Pew_new
                wrote on last edited by
                #8

                I think that the case with rectangle isn't a way. I need overlapping of entire Row1 by Canvas. Because I'm going to use whole space of Row1, from left to right. True, I can't figure out what I need to do for that.

                Richard DeemingR 1 Reply Last reply
                0
                • P Pew_new

                  I think that the case with rectangle isn't a way. I need overlapping of entire Row1 by Canvas. Because I'm going to use whole space of Row1, from left to right. True, I can't figure out what I need to do for that.

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #9

                  I can assure you, it is the way. :) Try it and see.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  P 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    I can assure you, it is the way. :) Try it and see.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    P Offline
                    P Offline
                    Pew_new
                    wrote on last edited by
                    #10

                    Yes, agree. That's fake way. There's no canvas at fake colored places. :) I need overlapping of entire Row1.

                    Richard DeemingR 1 Reply Last reply
                    0
                    • P Pew_new

                      Yes, agree. That's fake way. There's no canvas at fake colored places. :) I need overlapping of entire Row1.

                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #11

                      You can't have the canvas maintain its aspect ratio and cover the entire space at the same time. Either the canvas will be distorted, or there will be spaces around it. There's nothing "fake" about using a rectangle to fill the background of the row.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      P 1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        You can't have the canvas maintain its aspect ratio and cover the entire space at the same time. Either the canvas will be distorted, or there will be spaces around it. There's nothing "fake" about using a rectangle to fill the background of the row.


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        P Offline
                        P Offline
                        Pew_new
                        wrote on last edited by
                        #12

                        Richard>There's nothing "fake" about using a rectangle to fill the background of the row. It would be better if that Rectangle would help me to adapt a Canvas properly. :) I saw at least a few apps where Canvas was playing, so to speak, one of key roles and excellently adapted to the range of resolutions. From relatively older 1024x768 to modern 1920x1080. Unfortunately the history has no records by which way it was made.:confused: I've tried many variants of ViewBox/Canvas symbiosis and different Canvas aspect ratios, to no avail. Previously I thought that WPF (in contrast with WinForm) is more advanced stuff, at least in terms of adaptation/responsiveness. Now, I see it's not that.:thumbsdown: I guess there are ways to solve that. But they are not based on pure XAML.

                        L 1 Reply Last reply
                        0
                        • P Pew_new

                          Richard>There's nothing "fake" about using a rectangle to fill the background of the row. It would be better if that Rectangle would help me to adapt a Canvas properly. :) I saw at least a few apps where Canvas was playing, so to speak, one of key roles and excellently adapted to the range of resolutions. From relatively older 1024x768 to modern 1920x1080. Unfortunately the history has no records by which way it was made.:confused: I've tried many variants of ViewBox/Canvas symbiosis and different Canvas aspect ratios, to no avail. Previously I thought that WPF (in contrast with WinForm) is more advanced stuff, at least in terms of adaptation/responsiveness. Now, I see it's not that.:thumbsdown: I guess there are ways to solve that. But they are not based on pure XAML.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #13

                          You approached it all wrong. At a minimum, all your "graphics" should have been a "user control". You've mixed content for 3 rows. As mentioned, "margins" are for "touching up"; not "layout". Why "paths" instead of "images"? None of it makes any sense.

                          "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                          P 1 Reply Last reply
                          0
                          • L Lost User

                            You approached it all wrong. At a minimum, all your "graphics" should have been a "user control". You've mixed content for 3 rows. As mentioned, "margins" are for "touching up"; not "layout". Why "paths" instead of "images"? None of it makes any sense.

                            "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                            P Offline
                            P Offline
                            Pew_new
                            wrote on last edited by
                            #14

                            Gerry Schmitz> you approached it all wrong. OK, these words are just words. Could you show me correct approaches by means of uploading a working code (as it was done above, in previous posts). Let's suppose. It'll be "simpliest" task called " how to make Canvas to be fully adaptive/responsive to the range of resolutions e.g. 1024x768 ---> 1920x1080". Really have no clue how to do that by professional way. I would be much appreciated.

                            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