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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:23:00+00:00 2026-05-28T20:23:00+00:00

I created my own user control with dependency properties and added it to my

  • 0

I created my own user control with dependency properties and added it to my main window, where I now want to be able set the dependency properties. The properties are not taking the value I set in the XAML of the main window and I am not sure what I am missing. In the code I set the default value for the FillBrush property to Yellow. In the XAML I set it to Red. When I click the Test button, it shows the property to be Yellow. Here is the code:

Window XAML

<Window x:Class="Test.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:test="clr-namespace:Test"
    Title="Window2" Height="200" Width="600">

    <StackPanel>
        <test:TestUserControl x:Name="myControl"
            FillBrush="Red" VerticalAlignment="Center" Margin="20"/>
        <Button Height="24" Width="300" Content="Test" Click="Button_Click" />
        <TextBox x:Name="debugTextBox" Margin="20"/>
    </StackPanel>
</Window>

Window Code Behind

public partial class Window2 : Window
{
    public static readonly DependencyProperty FillBrushProperty =
        DependencyProperty.Register("FillBrush", typeof(Brush), typeof(Window2),
        new UIPropertyMetadata(Brushes.Yellow));

    public Brush FillBrush
    {
        get { return (Brush)GetValue(FillBrushProperty); }
        set { SetValue(FillBrushProperty, value); }
    }

    public Window2() { InitializeComponent(); }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.debugTextBox.Text = 
            "Red: " + Brushes.Red +
            "  Yellow: " + Brushes.Yellow +
            "  Actual: " + this.FillBrush;
    }
}

User Control XAML

<UserControl x:Class="Test.TestUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <TextBlock Text="User Control"/>
</UserControl>

User Control Code Behind

public partial class TestUserControl : UserControl
{
    public static readonly DependencyProperty FillBrushProperty =
        DependencyProperty.Register("FillBrush", typeof(Brush), typeof(TestUserControl),
        new UIPropertyMetadata(Brushes.Cyan));

    public Brush FillBrush
    {
        get { return (Brush)GetValue(FillBrushProperty); }
        set { SetValue(FillBrushProperty, value); }
    }

    public TestUserControl()
    {
        InitializeComponent();
    }
}

What am I missing?

  • 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-28T20:23:01+00:00Added an answer on May 28, 2026 at 8:23 pm

    In the XAML you are setting the Fillbrush value on TestUserControl to Red, but when the button is clicked it shows the Fillbrush value for Window2, not TestUserControl. And since the Fillbrush value for Window2 is not set in XAML it still has the default value of Yellow.

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

Sidebar

Related Questions

I have successfully created my own virtual path provider to load a user control
I've created my own signup form and creating the user using the Membership class.
I've created my own WCF service and I've successfully been able to talk to
Hi I have a mainView window which has its dataContext set to it's own
Maybe someone can help me out. I have created a simple web user control,
I create my own user control which only contains a panel : When I
I created a user control in Visual Studio in .Net framework 3.5 and using
I am attempting to write a 'User Control' in WinForms .NET (not ASP.NET). The
I created my own parental control app using C# to monitor my kids activity.
I have created my own control in WPF. Initially I created it as a

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.