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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:56:24+00:00 2026-05-14T06:56:24+00:00

I want to use a dependency property so that my label displays values selected

  • 0

I want to use a dependency property so that my label displays values selected in the Listbox. This is just to more clearly understand the working of a dependency property.

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WPFToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    xmlns:local="clr-namespace:WpfApplication1"
    x:Name="MyWindow"
    Height="200"
    Width="300">
       <StackPanel>
        <ListBox x:Name="lbColor"
                   Width="248"
                   Height="56"
                   ItemsSource="{Binding TestColor}"/>
       <StackPanel>
      <Label Content="{Binding Path=Test, ElementName=lbColor}" />
    </StackPanel>
  </StackPanel>
</Window>

Code Behind,

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {

        public ObservableCollection<string> TestColor { get; set; }

        public String Test
        {
            get { return (String)GetValue(TestProperty); }
            set { SetValue(TestProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TestProperty =
            DependencyProperty.Register("Test", typeof(String), typeof(ListBox), new UIPropertyMetadata("Test1"));



        public Window1()
        {
            InitializeComponent();
            TestColor = new ObservableCollection<string>();
            DataContext = this;
            TestColor.Add("Red");
            TestColor.Add("Orange");
            TestColor.Add("Yellow");
            TestColor.Add("Green");
            TestColor.Add("Blue");
        }
    }
}

Can anyone explain to me how will I accomplish this using a dependency property? Somehow I am very confused with the Dependency Property concept, and I just wanted to see a working example for that.

  • 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-14T06:56:24+00:00Added an answer on May 14, 2026 at 6:56 am

    You’ll need to have your ListBox “select” the current text:

    <StackPanel>
        <!-- Add selected item binding -->
        <ListBox 
             x:Name="lbColor" Width="248" Height="56" 
             ItemsSource="{Binding TestColor}"
             SelectedItem="{Binding Test}"
        />
    
        <StackPanel>
            <!-- No need for elementname - just use Test on the DataContext -->
            <Label Content="{Binding Path=Test}" />
        </StackPanel>
    </StackPanel>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to convert this property to a dependency property ? Regarding that everybody just
How to bind non-dependency Text property to Application.Current.Resources using XAML? I want to use
I want to use dependancy injection technique in my site. For that i want
I have a transaction log file in CSV format that I want use to
I have a control with a dependency property which I want to pass which
This may seem obvious to most people, but I'm just trying to confirm that
I want to use code similar to that found here. The issue I'm having
I have just started to create a user control realising that I want a
I'm using a PasswordBox which exposes a dependency property such that I can bind
I've got a webforms app that I'd like to use dependency injection on. I

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.