locked
Set style for Control in UserControl

    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) :

    <UserControl
        x:Class="Test.WrapperPasswordBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Test"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        
        <Grid>
            <PasswordBox x:Name="PasswordBox" />
        </Grid>
    </UserControl>
    

    2 - I create a Style in App.xaml:

                <Style TargetType="PasswordBox"
                       x:Key="GlobalPasswordBox">
                    <Setter Property="FontSize"
                            Value="30" />
                    <Setter Property="Height"
                            Value="50" />
                </Style>

    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
    Moderator
  • 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.

    this.passwordbox.Name_Method_The_Same_Name_Method_Of_PasswordBox(Paramater_The_Same);

    }

    }

    Tuesday, February 10, 2015 9:27 AM