Set style for Control in UserControl RRS feed

  • Question

  • I have a issue with set style for Control in UserControl.

    1 - I create a UserControl wrapper PasswordBox(need to override some method for PasswordBox) :

            <PasswordBox x:Name="PasswordBox" />

    2 - I create a Style in App.xaml:

                <Style TargetType="PasswordBox"
                    <Setter Property="FontSize"
                            Value="30" />
                    <Setter Property="Height"
                            Value="50" />

    3 - I replace PasswordBox with WrapperPasswordBox in MainPage.xaml :

    <PasswordBox Style="{StaticResource GlobalPasswordBox}">
    <WrapperPasswordBox Style="{StaticResource GlobalPasswordBox}">

    My issue is : When i run to MainPage.xaml, application will crash.

    I know : can't set style of PasswordBox for UserControl. But i can't move Style in MainPage.xaml to PasswordBox of UserControl because i have another style for PasswordBox in another Page.

    Do you have any suggestion for this issue ?

    Friday, January 30, 2015 3:58 AM

All replies

  • Why not set the style on the PasswordBox inside the UserControl?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, January 30, 2015 2:40 PM
  • Hi Matt Small.

    Example :

    In MainPage.xaml

    <SecurePasswordBox Style="{StaticResource BoldStyle}">

    In DisplayScreen.xaml

    <SecurePasswordBox Style="{StaticResource ItalicStyle}">

    How to resolved it ? One style for one Page.

    Monday, February 2, 2015 4:47 AM
  • I tried to create a new class to inherit the PasswordBox control, but I found that class is sealed. How can you inherit it and then override its methods? https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.passwordbox.aspx.

    Provide more to shows your reproduction.

    Monday, February 9, 2015 6:31 AM
  • Let see example :

    Class SecurePasswordBox


    PasswordBox passwordbox;

    public SecurePasswordBox(PasswordBox passwordbox) 


    this.passwordbox = passwordbox;


    // You will define method the same as PasswordBox

    // Example

    public Name_Method_The_Same_Name_Method_Of_PasswordBox(Paramater_The_Same)


    // You wiil make your code here.




    Tuesday, February 10, 2015 9:27 AM