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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:32:46+00:00 2026-06-15T16:32:46+00:00

I have created a custom control which displays a number pad. The control has

  • 0

I have created a custom control which displays a number pad. The control has a dependency property (ButtonWidth) which sets the key size for all keys in the number pad. When the property changes all child buttons are enumerated and their Height and Width properties are updated.

At design time this works fine. I can change the property and the number pad display changes accordingly.

However at runtime the number pad is created but the button widths are not updated. I added a button which set the width in the Click event and this worked.

public static readonly DependencyProperty ButtonWidthProperty =
     DependencyProperty.Register("ButtonWidth", 
        typeof(int),
        typeof(VirtualKeyboard), 
        new FrameworkPropertyMetadata(40,
            FrameworkPropertyMetadataOptions.AffectsArrange |
            FrameworkPropertyMetadataOptions.AffectsMeasure |
            FrameworkPropertyMetadataOptions.AffectsRender |
            FrameworkPropertyMetadataOptions.AffectsParentMeasure |
            FrameworkPropertyMetadataOptions.AffectsParentArrange,
        OnButtonWidthPropertyChanged, OnCoerceButtonWidthProperty),
        OnValidateButtonWidthProperty);

public int ButtonWidth
{
    get { return (int)GetValue(ButtonWidthProperty); }
    set { SetValue(ButtonWidthProperty, value); }
}

private static void OnButtonWidthPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
    Console.WriteLine("VK width");
    VirtualKeyboard control = source as VirtualKeyboard;
    int newVal = (int)e.NewValue;
    control.UpdateButtons();
}

private static object OnCoerceButtonWidthProperty(DependencyObject sender, object data)
{
    return data;
}

private static bool OnValidateButtonWidthProperty(object data)
{
    return data is int;
}

public VirtualKeyboard()
{
    Console.WriteLine("VK constr");
    InitializeComponent();
}

protected override void OnInitialized(EventArgs e)
{
    base.OnInitialized(e);
    isCaps = true;
    SetKeys();
    UpdateButtons();  // this is where the current ButtonWidth property 
                      // is read and the button width set
}

private void UpdateButtons()
{
    Console.WriteLine("VK bw=" + ButtonWidth);

    foreach (Button button in FindVisualChildren<Button>(this))
    {
        button.Width = button.Height = ButtonWidth;
    }
}

What I noticed is that if I also set the Content property of a button this seems to force a re-layout of the control.

What am I doing wrong here? Why does it work at design time but not at run time?

  • 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-15T16:32:47+00:00Added an answer on June 15, 2026 at 4:32 pm

    Try doing your updating of the Width and Height of your Buttons after the custom control has been loaded i.e. in the Loaded event….not in your OnInitialized….because you need to wait for the template to be applied and the buttons to be created and in your visual tree.

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

Sidebar

Related Questions

I have created a user control which exposes a custom type property called SoftwareItem.
I have created a custom control but I can't bind a property to the
I am working on a custom control and have created a property in property
I have created a custom control which derives from Panel. I use it to
i have created a custom user control which im using on my main xaml
I have a custom control in which I created a custom DependencyProperty called TheObject
I have created a simple Asp.Net custom control which automatically combines all the correct
I created a custom control which contains an Image control. I'd like to have
I have created a custom control which uploaded files to the server . These
I have created the custom control which is just a panel that I will

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.