Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8352189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:53:39+00:00 2026-06-09T08:53:39+00:00

I’m trying to add a simple visual to my application representing the print margins

  • 0

I’m trying to add a simple visual to my application representing the print margins on a page to help a user avoid positioning data that might be out of range. To do this, I have a method which takes paper size, DPI, and margin settings information and uses it to make an ObservableCollection(of Rect) which contains four Rect’s, one for each edge of the page. A ListBox in XAML is bound to the collection by way of a GetMargins() property that returns the collection. I have defined a DataTemplate for the Page in the Resource section of the Grid hosting the ListBox, and the ListBox uses a Canvas in ItemsPanelTemplate so that I can position and size some Rectangles on the page using each Rect’s x, y, width, and height. Here’s the XAML:

In Grid.Resources:

<!--
A data template for the page margins.
-->
<DataTemplate DataType="{x:Type local:Page}">
    <Grid>
        <Rectangle
            Width="{Binding Width}"
            Height="{Binding Height}"
            Fill="LightGreen"
            Opacity="0.5"
            />
    </Grid>             
</DataTemplate>

In Grid:

<!--
Listbox to present the page margins.
-->
<ListBox
    x:Name="PageMarginsListBox"
    ItemsSource="{Binding GetMargins}"
    Background="Transparent"
    IsHitTestVisible="False"
    >
    <ListBox.ItemsPanel>
         <ItemsPanelTemplate>
              <Canvas />
         </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemContainerStyle>
         <Style TargetType="ListBoxItem">
              <Setter
                   Property="Canvas.Left"
                   Value="{Binding X}"                        
                   />
              <Setter
                   Property="Canvas.Top"
                   Value="{Binding Y}"                        
                   />
         </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

When I run the application, the above code results in string output instead of light green rectangles on the page’s borders. For example, the Rect for the left-side margin will appear as “0,0,75,1650” instead of a rectangle that is 75px wide and spans the height of the whole page. The bottom margin string is positioned in the bottom left corner, and the right margin appears in the top right corner, as expected. So the binding is working and they are positioned correctly, but not drawn for some reason.

What I find interesting about this is that I have another ObservableCollection filled with objects of my own design, which are positioned and styled in almost the same way on the Page’s surface. The XAML for the ListBox bound to that collection is set up exactly the same way, but with a somewhat more complex DataTemplate for each item (9 rectangles instead of just one). It works quite well. They both live in the same Grid, too.

So I don’t see where I’m going wrong with this one, except that it’s probably a DataTemplate issue if I’m getting toString() output. Thoughts? Spot my mistake?

Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:53:40+00:00Added an answer on June 9, 2026 at 8:53 am

    It seems to me You have wrong DataType in DataTemplate. GetMargins() returns ObservableCollection of Rect right? Then ListboxItems are created from Rect’s while Your DataTemplate is defined for Page class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.