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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:13:00+00:00 2026-05-25T18:13:00+00:00

I have a user-control and I want to use it in some other project.

  • 0

I have a user-control and I want to use it in some other project. There is no problem when I set some value to its properties directly:

<local:MyUserControl prop1="val1" prop2="val2">
    ...
</local:MyUserControl>

But I can’t apply a style to it. I tried:

<Window ...>
    <Window.Resources>
        <Style x:Key="MyUserControlStyle" TargetType="{x:Type local:MyUserControl}">
            <Setter Property="prop1" Value="val1"/>
            <Setter Property="prop2" Value="val2"/>
        </Style>
    </Window.Resources>

    <Grid>
        <local:MyUserControl Style="{StaticResource ResourceKey=MyUserControlStyle}">
            ...
        </local:MyUserControl>
    </Grid>
</Window>

Where did I wrong? -Thanks

  • 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-25T18:13:01+00:00Added an answer on May 25, 2026 at 6:13 pm

    Using dear @Mario Vernari’s instructions, I found it out that the problem was due to a bad strategy which I’d used to create my UserControl. I wanted to create a UserControl that be able to hold some other ones. So I had tried this:

    <UserControl x:Class="MyNamespace.MyUserControl"
                 ...
                 Style="{DynamicResource ResourceKey=MyUserControlStyle}">
        <UserControl.Resources>
            ...
            <Style x:Key="MyUserControlStyle" TargetType="{x:Type UserControl}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type UserControl}">
                            <Border BorderBrush="{Binding Path=DP1}">
                                ...
                                <ContentPresenter ... Content="{TemplateBinding Content}"/>
                                ...
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </UserControl.Resources>
    </UserControl>
    

    Where DP1 is a dependency property of type Brush. The UserControl which has been created through this way works if you set its properties (like DP1) directly. Absolutely this is not the true way as @Mario told me:

    …When you use an UserControl, it means that you already know its layout, and there is no need to style the control further. You are defining its style twice at the same time thus results a collision…

    And he added:

    Instead, you should use a CustomControl; Define the default style in the Themes folder (if you own regular Visual Studio, it makes automatically). Afterward, you may override the default style in your own app. In the same way you would do for a normal base class and its derived.

    Follow this:
    http://www.codeproject.com/KB/WPF/WPFCustomControl.aspx …

    Obviously, in this case we need to derive our lookless control from ContentControl class (instead of Control class). You may take a look at this & this to master the details.

    Here, I give thanks to @Mario again. 😉

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

Sidebar

Related Questions

I have an asp.net user control on which I want to use jquery ajax.
I have two User Controls already created now I want to use one as
i have user control, which i render on several views. i want to show
I have an ASP.NET user control (.ascx file). In this user control I want
I have a user control that contains multiple controls (CheckBox, Button, Label...). I want
I have a page (A) and a user control and I want to send
I have a tree control and I want to give the user the ability
I have asp:DropDownList control which i want to display data to user who don't
I have a little problem with the creation of a user control. Though I
I have a custom UserControl called (for instance) MyPanel, and I want to use

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.