How to get d:DesignWidth ="100" from code behind

BitSmithy 2,141 Reputation points
2023-12-07T10:59:39.8333333+00:00

Hello,

Under design time, I can set in XAML such properties (to Page or UserControl)

        d:DesignWidth ="100"
        d:DesignHeight ="100"
        ```

How to get and set these properties from code behind?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 19,936 Reputation points Microsoft Vendor
    2023-12-08T02:25:15.5366667+00:00

    Hello @BitSmithy ,

    Welcome to Microsoft Q&A!

      mc:Ignorable="d"
      d:DesignHeight="100"
      d:DesignWidth="100"
    

    According to the explanation in the XAML namespaces, currently we cannot get the properties after mc:Ignorable.

    " mc:" indicates and supports a markup compatibility mode for reading XAML. Typically, the "d:" prefix is associated with the attribute mc:Ignorable. This technique enables run-time XAML parsers to ignore the design attributes in "d:".

    If you want to get the height and width in the xaml resource of Page or UserControl, please use Width and Height directly instead of d:DesignHeight and d:DesignWidth.

    <UserControl
        x:Class="XamlTest.MyUserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:XamlTest"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" 
        Width="100"
        Height="100">
    
     var height = myControl.ActualHeight;
     var width = myControl.ActualWidth;
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.