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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:53:06+00:00 2026-05-30T09:53:06+00:00

New to WPF. I am creating UserControls that need read access to the ViewModel

  • 0

New to WPF. I am creating UserControls that need read access to the ViewModel state to do their thing. I currently use the following technique:

public partial class ControlBar : UserControl
{

    private static readonly DependencyProperty URLProperty =
        DependencyProperty.Register("URL", typeof(string), typeof(ControlBar),
        new UIPropertyMetadata(null));

    public ControlBar()
    {
        InitializeComponent();

        SetBinding(URLProperty, "CurrentPage.URL");

        Pin.Click += Pin_Click;       
    }

    private void Pin_Click(object sender, RoutedEventArgs e)
    {
        var URL = (string)GetValue(URLProperty);

    }
}

Is this the correct way and is it not overkill to set up a long-term binding for each variable I need access to? Or can you do something like:

GetValue(new Path("CurrentPage.URL..... 

I made up the above obviously.

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-30T09:53:07+00:00Added an answer on May 30, 2026 at 9:53 am

    In general data-binding is the way to go. However sometimes when you are creating controls that have view-specific concerns for which data-binding will not be appropriate.

    In those cases you will want to be able to interact with the DependencyProperty to set it and know when it changes. I have been following a pattern that I picked up from a Charles Petzold article in MSDN magazine.

    My answer to another question shows the pattern for creating a DependencyProperty for a UserControl Stack Overflow: Dependency Property In WPF/SilverLight

    Again, data-binding to a view model will likely solve your problem, but a DependencyProperty may come in useful depending on the situation.

    Update in response to comment:

    In many situations you can data bind your in a UserControl without using a DependencyProperty. For example if you have a TextBlock that displays a name you would put a TextBlock in the XAML of the UserControl

    <TextBlock Text="{Binding Path=NameString}" />
    

    In the view model which is present in the DataContext you would have a property NameString and if the TextBlock is to update the display when the NameString property changes the view model should implement INotifyPropertyChanged and the property should fire the PropertyChanged event with the name of the property sent along with the event.

    protected string _NameString;
    public string NameString
    {
        get { return _NameString; }
        set { _NameString = value: Notify("NameString"); }
    }
    

    Where Notify is a method that checks the PropertyChanged event for null and sends the event if not null.

    This works well if everywhere that you want to use the UserControl has a view model with a Name property. The great thing is that the UserControl can pick up on the DataContext of wherever it is hosted and bind to an external view model.

    When you want to start binding the same UserControl to different properties is one place that you may want to use a DependencyProperty. In that case you could make a UserControl with a DependencyProperty and bind it to different properties

    <my:SampleControl NameString="{Binding Path=GivenName}" />
    <my:SampleControl NameString="{Binding Path=FamilyName}" />
    

    And then have an internal view model that the DependencyProperty change handler updates when the bound property changes.

    Update: No DependencyProperty or binding

    You can always add an ordinary C# property to the UserControl and pass the data in that way.

    public MyClass Data { get; set; }
    

    Then in the code-behind of the UserControl you can simply use the property:

    if (this.Data != null)
    {
        this.textBox1.Text = Data.NameString;
    }
    

    Update in response to comment:

    Another way to access the view model in code is to cast the DataContext to your view model type:

    MyClass data = this.DataContext as MyClass;
    if (data != null)
    {
        // do something
        this.textBox1.Text = data.NameString;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a logger application thing to learn WPF, and I want new messages
I'm creating a new project that is following closely to the design used by
I am creating a WPF-usercontrol that needs to check that new UIElements does not
I am creating a new WPF window that I want to be parented to
I am creating a WPF form. One of the requirements is that it have
I'm new to using WPF. I have the following program I want to create:
I'm relatively new to .Net 4 and I am creating my FIRST WPF application
I'm creating an options dialog using WPF that lists possible keys so that the
I am new to WPF and I am creating an application which uses the
Just fyi, I am new to WPF. I am creating a sidebar in my

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.