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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:16:37+00:00 2026-05-23T23:16:37+00:00

I am trying to create a composite DataContext for a UserControl. Basically I have

  • 0

I am trying to create a composite DataContext for a UserControl. Basically I have a control which has Order and Package properties and I wanted to create the composite object representing this datasource in XAML rather than in code.

This is how I am trying to display the UserControl (and create the DataContext):

<views:PackageDetailsControl>
    <views:PackageDetailsControl.DataContext>
        <vm:OrderPackagePair Package="{Binding Package, Mode=OneWay}" 
                             Order="{Binding Order, Mode=OneWay}"/>                 
    </views:PackageDetailsControl.DataContext>
</views:PackageDetailsControl>  

The OrderPackagePair object is a simple dependency object that is created in XAML :

public class OrderPackagePair : DependencyObject
{
    public OrderDetails Order
    {
        get { return (OrderDetails)GetValue(OrderProperty); }
        set { SetValue(OrderProperty, value); }
    }

    public static readonly DependencyProperty OrderProperty =
        DependencyProperty.Register("Order", typeof(OrderDetails), typeof(OrderPackagePair), new UIPropertyMetadata(null));

    public PackageInfo Package
    {
        get { return (PackageInfo)GetValue(PackageProperty); }
        set { SetValue(PackageProperty, value); }
    }

    public static readonly DependencyProperty PackageProperty =
        DependencyProperty.Register("Package", typeof(PackageInfo), typeof(OrderPackagePair), new UIPropertyMetadata(null));
}

Order and Package are not bound correctly and are just null.

Yes I know there’s probably a better way of doing this – but I cannot understand why this isn’t working. Occasionally in Blend it’ll work and then go blank again.

  • 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-23T23:16:38+00:00Added an answer on May 23, 2026 at 11:16 pm

    This will not work because DependencyObject(OrderPackagePair class) doesn’t monitor internal changes of its dependency properties. As OrderPackagePair object remains the same, DataContext considered as unchanged.

    On the opposite site, class Freezable is intented to notify subscribers that instance was changed when one of its dependency properties changed.

    So, try to declare Freezable instead of DependencyObject as base class of OrderPackagePair.

    ————- UPDATE ——–

    Yes, it works. In order to prove it I’ve implemented simple example.

    Code of OrderPackagePairClass:

    public class OrderPackagePair : Freezable
    {
        public OrderDetails Order
        {
            get { return (OrderDetails)GetValue(OrderProperty); }
            set { SetValue(OrderProperty, value); }
        }
    
        public static readonly DependencyProperty OrderProperty =
            DependencyProperty.Register("Order", typeof(OrderDetails), typeof(OrderPackagePair), new UIPropertyMetadata(null));
    
        public PackageInfo Package
        {
            get { return (PackageInfo)GetValue(PackageProperty); }
            set { SetValue(PackageProperty, value); }
        }
    
        public static readonly DependencyProperty PackageProperty =
            DependencyProperty.Register("Package", typeof(PackageInfo), typeof(OrderPackagePair), new UIPropertyMetadata(null));
    
        protected override Freezable CreateInstanceCore()
        {
            throw new NotImplementedException();
        }
    }
    

    XAML:

    <Window x:Class="WindowTest.MainWindow"
            xmlns:self="clr-namespace:WindowTest"
            Name="RootControl">
        <StackPanel Margin="10" DataContextChanged="StackPanel_DataContextChanged">
            <StackPanel.DataContext>
                <self:OrderPackagePair Package="{Binding Path=DataContext.PackageInfo, Mode=OneWay, ElementName=RootControl}" 
                                       Order="{Binding Path=DataContext.OrderDetails, Mode=OneWay, ElementName=RootControl}"/>
            </StackPanel.DataContext>
    
            <Button Margin="10" Content="Change Package" Click="Button_Click"/>
        </StackPanel>
    </Window> 
    

    And code behind:

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }
    
        private OrderDetails _orderDetails;
        public OrderDetails OrderDetails
        {
            get
            {
                return this._orderDetails;
            }
            set
            {
                this._orderDetails = value;
                this.OnPropertyChanged("OrderDetails");
            }
        }
    
        private PackageInfo _packageInfo;
        public PackageInfo PackageInfo
        {
            get
            {
                return this._packageInfo;
            }
            set
            {
                this._packageInfo = value;
                this.OnPropertyChanged("PackageInfo");
            }
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.PackageInfo = new PackageInfo(DateTime.Now.ToString());
        }
    
        private void StackPanel_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            Trace.WriteLine("StackPanel.DataContext changed");
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            var safeEvent = this.PropertyChanged;
            if (safeEvent != null)
            {
                safeEvent(this, new PropertyChangedEventArgs(name));
            }
        }
    }
    

    When you click the button, model changes PackageInfo property (for simplicity model and view are implemented in the same class). Dependency property OrderPackagePair.Package reacts on new value and overwrites its value. Due to Freezable nature, OrderPackagePair notifies all subscribers that it was changed and handler StackPanel_DataContextChanged is called. If you get back to DependencyObject as base class of OrderPackagePair – handler will be never called.

    So, I suppose your code doesn’t work because of other mistakes. You should carefully work with DataContext. For example, you wrote:

    <views:PackageDetailsControl>
        <views:PackageDetailsControl.DataContext>
            <vm:OrderPackagePair Package="{Binding Package, Mode=OneWay}" 
                                 Order="{Binding Order, Mode=OneWay}"/>                 
        </views:PackageDetailsControl.DataContext>
    </views:PackageDetailsControl>
    

    and certainly this is one of the problems. Binding expression is oriented on current DataContext. But you set DataContext as OrderPackagePair instance. So you binded OrderPackagePair.Package to OrderPackagePair.Package (I suppose, that your goal is to bind OrderPackagePair.Package to Model.Package). And that’s why nothing happened.

    In my example in binding expression I explicitly tell to which DataContext I want to bind:

     Package="{Binding Path=DataContext.PackageInfo, Mode=OneWay, ElementName=RootControl}"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a composite ASP.NET control that let's you build an editable
I'm trying to create a table in SQL Server 2000, that has a composite
I am trying to create a composite key that mimicks the set of PrimaryKeys
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Trying to create a user account in a test. But getting a Object reference
I'm trying to create a composite component for use in my Seam application, and
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I'm trying to create an Eclipse Form that has three foldable sections, one fixed
I'm trying create a composite component to use across my projects, so, I've created
i am trying to create a football simulation program. i have a main class

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.