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 7566293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:22:34+00:00 2026-05-30T14:22:34+00:00

Recently I discovered WPF supports different measurement units in XAML. Besides default DIPs, there

  • 0

Recently I discovered WPF supports different measurement units in XAML. Besides default DIPs, there is also support for pixels, inches and centimeters (as far as I know). This allows designer to write XAML such as this:

<Canvas>
    <Line X1="0cm" X2="3cm" Y1="1cm" Y2="3cm" Stroke="Black"/>
</Canvas>

However, you cannot bind these values. Imagine we have a ViewModel with Dimension property which is a String, for example “7cm”. Following won’t work:

<Button Width="{Binding Dimension}">Test</Button>

FormatException gets thrown. Similarly, when creating a FrameworkElement in code-behind, like this:

Canvas1.Children.Add(new Button() { Width = "3cm", Content = "Test"});

Compilation fails because exception is thrown in constructor/wherever you try to create the control.

I wonder:

  • Is it possible to use natural units (in my case metric – centimeters) in code-behind?
  • How?
  • Is there a complete list of units WPF/XAML supports?

EDIT:

Here is a link from comment below, which resolves this question:

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.width.aspx

  • 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-05-30T14:22:35+00:00Added an answer on May 30, 2026 at 2:22 pm

    You can do this in code behind by applying the same conversion the XAML-parser uses, you do not need to reimplement this logic if you don’t mind passing around strings:

    button.Width = (double)new LengthConverter().ConvertFrom("2cm");
    

    The XAML-parser uses TypeConverters to convert strings to the needed values, in the documentation page of the Width property you can see that it has a TypeConverterAttribute specifying that a LengthConverter should be used. This attribute can be used to locally override how a property should be handled, but it can also be applied at class level, so if an Employee class has this attribute specifying that an EmployeeConverter should be used said converter will be the default for properties of type Employee.

    I am a bit surprised the binding does not apply this type converter, but using it inside an IValueConverter would be simple enough, in fact you could create a markup extension which constructs an IValueConverter from a type converter to make it nicely generic.

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

Sidebar

Related Questions

I recently discovered that relying on default encoding of JVM causes bugs. I should
I've recently discovered that there are several useful templates (in Eclipse) that can be
We recently discovered that the Google Maps API does not play nicely with SSL.
I recently discovered the genshi.builder module. It reminds me of Divmod Nevow's Stan module.
Having recently discovered this method of development, I'm finding it a rather nice methodology.
I recently discovered Erlang and am now working my way through a couple of
I recently discovered metaclasses in python. Basically a metaclass in python is a class
I recently discovered that when you have a page set to session readonly and
I recently discovered that I can use lambdas to create simple event handlers. I
I recently discovered that a sql query that was running fine earlier is now

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.