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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:20:43+00:00 2026-05-25T00:20:43+00:00

I have a control which has a button named btn1, and I want to

  • 0

I have a control which has a button named “btn1”, and I want to change it’s contents through a dependency property in xaml, like this:

<UserControl:UserControl1 ButtonContents="Something"/>

Here’s what I have:

Public Class UserControl1 
    Public Shared ReadOnly ButtonContentsProperty As DependencyProperty =
        DependencyProperty.Register("ButtonContents",
                                    GetType(String),
                                    GetType(UserControl.UserControl1))

    Public Property ButtonContents() As Boolean
        Get
            Return GetValue(ButtonContentsProperty)
        End Get
        Set(ByVal value As Boolean)
            SetValue(ButtonContentsProperty, value)
        End Set
    End Property
End Class

But how can the dependency property know what to do?

  • 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-25T00:20:43+00:00Added an answer on May 25, 2026 at 12:20 am

    The solution is based upon the following approach – button’s content gets defined as a resource belonging to the button itself. Unfortunately ResourceKey is not a DP and hence cannot be bound, we created an attached property BindiableResourceKey which subsistutes for that. The user control has a property ButtonLook of string type which holds the name of the resource to be used as button’s content. If you need to implement more complex linking logic just extend the attached property value changed handler.

    Here’s the code:

    Part1 – User Control:

    <UserControl x:Class="ButtonContentBinding.AControl"
                 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"
                 xmlns:local="clr-namespace:ButtonContentBinding">
        <Grid>
            <Button local:BindableResourceControl.ResourceKey="{Binding ButtonLook}">
                <Button.Resources>
                    <Rectangle x:Key="BlueRectangle"
                        Width="40" Height="40" Fill="Blue" />
                    <Rectangle x:Key="GreenRectangle"
                        Width="40" Height="40" Fill="Green" />
                </Button.Resources>
            </Button>
        </Grid>
    </UserControl>
    

    Part 2 – Attached Property:

    public class BindableResourceControl : DependencyObject
        {
            public static readonly DependencyProperty ResourceKeyProperty =
                DependencyProperty.RegisterAttached("ResourceKey",
                typeof(string),
                typeof(BindableResourceControl),
                new PropertyMetadata((x, y) =>
                {
                    ContentControl contentControl = x as ContentControl;
    
                    if (x != null)
                    {
                        contentControl.Content = contentControl.Resources[y.NewValue];
                    }
                }));
    
            public static void SetResourceKey(DependencyObject x, string y)
            {
                x.SetValue(BindableResourceControl.ResourceKeyProperty, y);
            }
    
            public static string GetResourceKey(DependencyObject x)
            {
                return (string)x.GetValue(BindableResourceControl.ResourceKeyProperty);
            }
        }
    

    Part 3 – Consumer:

    <Window x:Class="ButtonContentBinding.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:ButtonContentBinding">
        <Grid>
            <my:AControl ButtonLook="GreenRectangle"
                    HorizontalAlignment="Left" Margin="0"      
                    x:Name="aControl1" VerticalAlignment="Top" 
                    Height="200" Width="200" />
        </Grid>
    </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a control which has a button on it. All the button does
I have a JQuery control which has some private methods. I would like to
I have user control named DateTimeUC which has two textboxes on its markup: <asp:TextBox
I have a form which has only tab control in it. I want to
I have a silverlight control which has a reference to a silverlight enabled wcf
I have a user control which has a textbox on it, now this usercontrol
I have a user control which has a DateTimePicker overlaid with the single line
I have a requirement to create a windows form control which has to detect
I have a maximized form which has Combo-Box control (docked in top-right) with 500px
I have a form which has a Combo Box Control. I have selected the

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.