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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:15:07+00:00 2026-05-17T00:15:07+00:00

I have a UserControl which contains 4 ToggleButtons and I’d like to trigger a

  • 0

I have a UserControl which contains 4 ToggleButtons and I’d like to trigger a custom event that an interested object can listen for which provides a status, based on the ToggleButton Checked values and also value(s) from the DataContext object.

Getting the ToggleButton checked values and deriving a status is simple enough, however I can’t work out how I access the DataContext object within the C# codebehind.

For example, if an interested object receives the RoutedEvent from the UserControl, I would like it to be able to access values from the UserControl’s DataContext object.

Will I need to expose specific properties from the DataContext object or can I somehow expose the DataContext object from the UserControl’s API?

Update.

To explain the problem a little more, I have a list of items which creates a set of UserControl instances in a container, I attach event listeners to each item as it’s added to the container and send an event from one of the UserControls when it’s child controls are clicked / checked etc.

Getting a reference to the UserControl that dispatched the event is straightforward enough, but I can’t access the DataContext object, do I need to assign a public property to expose the DataContext object …

e.g.

private ControlViewModel myControlViewModel;
public ControlViewModel MyControlViewModel {
    get { return myControlViewModel; }
    set 
    {   
        this.DataContext = value;
        myControlViewModel = value;
    }
}

or is there a better way?

Any tips would be appreciated, Thank you.

  • 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-17T00:15:08+00:00Added an answer on May 17, 2026 at 12:15 am

    Well, it looks like I should’ve tried the simplest solution first…

    …of course I can access the DataContext object like this:

    (userControl.DataContext as ControlViewModel).requiredProperty;
    

    Update

    So I ended up passing the DataContext view model reference via a event/delegate pair like this…

    public delegate void StatusChangedHandler(string status, UserControlViewModel model);
    
    public event StatusChangedHandler StatusChanged;
    

    And then just invoked the event like this…

    StatusChanged.Invoke("message", DataContext as UserControlViewModel) 
    // or DataContext as IUserControlModelInterface
    

    Which allowed me to adequately aggregate events from the UserContol’s child controls, and access the DataContext model from an event handler.

    I still wonder if there is a more best practice way to do this?

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

Sidebar

Related Questions

I have a UserControl which contains a TextBox and a CustomValidator. I would like
I have a UserControl which contains 3 labels. I want to add an event
I have UserControl which contains a TextBox and a Button control. The Button opens
I have a UserControl which contains voting buttons inside an UpdatePanel, and outside a
I have a UserControl which contains a TextBox . When my main window loads
I have a UserControl which contains a listbox and few buttons. <UserControl x:Class=ItemControls.ListBoxControl xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
Hi. I have a UserControl which contains a textbox. I wanted to access the
I have a UserControl A which contains a dropdown a placeholder At runtime the
I am using C# + winforms to develop software. I have a UserControl which
I created a custom ComboBox as follows: (note, code is not correct but you

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.