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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:05:09+00:00 2026-06-04T22:05:09+00:00

I have two UserControls: public partial class MKSelectMonthUC : UserControl { public static readonly

  • 0

I have two UserControls:

public partial class MKSelectMonthUC : UserControl
{
    public static readonly DependencyProperty CurrentYearProperty = DependencyProperty.Register("CurrentYear", typeof(int), typeof(MKSelectMonthUC), new PropertyMetadata(0));
    public int CurrentYear
    {
        get { return (int)GetValue(CurrentYearProperty); }
        set
        {
            SetValue(CurrentYearProperty, value);
        }
    }

    public static readonly DependencyProperty ChatRoomIdProperty = DependencyProperty.Register("ChatRoomId", typeof(int), typeof(MKSelectMonthUC), new PropertyMetadata(0));
    public int ChatRoomId
    {
        get { return (int)GetValue(ChatRoomIdProperty); }
        set
        {
            SetValue(ChatRoomIdProperty, value);
            if(value > 0)
                GetChatReport(ChatRoomId);
        }
    }

}

and

public partial class MKSelectPeriodForChatReportCW : ChildWindow
{
    public static readonly DependencyProperty ChatRoomIdProperty = DependencyProperty.Register("ChatRoomId", typeof(int), typeof(MKSelectPeriodForChatReportCW), new PropertyMetadata(0));
    public int ChatRoomId
    {
        get { return (int)GetValue(ChatRoomIdProperty); }
        set
        {
            SetValue(ChatRoomIdProperty, value);
        }
    }

    public static readonly DependencyProperty CurrentYearProperty = DependencyProperty.Register("CurrentYear", typeof(int), typeof(MKSelectPeriodForChatReportCW), new PropertyMetadata(0));
    public int CurrentYear
    {
        get { return (int)GetValue(CurrentYearProperty); }
        set
        {
            SetValue(CurrentYearProperty, value);
        }
    }

}

in XAML of MKSelectPeriodForChatReportCW I want to bind it’s DependencyProperties to MKSelectMonthUC DependencyProperties like that:

<controls:ChildWindow x:Class="XX.mkControls.MKSelectPeriodForChatReportCW"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
       xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" 
       xmlns:mk="clr-namespace:XX.mkControls.MKSelectPeriodForChatReport"
       Name="mainControl"
       >
<Grid x:Name="LayoutRoot" Margin="2">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <mk:MKSelectMonthUC CurrentYear="{Binding CurrentYear, ElementName=mainControl}" ChatRoomId="{Binding ChatRoomId, ElementName=mainControl}" />


</Grid>

Properties on MKSelectPeriodForChatReportCW do get values (from their bindings) but values on MKSelectMonthUC don’t. So please help me find a solution. 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-06-04T22:05:11+00:00Added an answer on June 4, 2026 at 10:05 pm

    I don’t have all your source code and I cannot reproduce your problem, so all I can do is to make some suggestions based on the style of code you’ve presented above.

    1. As HiTechMagic says, the GetChatReport method call in the ChatRoomId setter in MKSelectMonthUC won’t get called as often as you might expect. If you want a method to be called every time a dependency property changes, use a PropertyChangedCallback. This page on MSDN has an example of how to use a PropertyChangedCallback.

      For a property backed by a dependency property, the getter should only contain a call to GetValue and the setter should only contain a call to SetValue.

    2. Bindings using ElementName can be awkward to work with because they are silent if something goes wrong (e.g. no element with the given name was found). Presumably you have values for your CurrentYear and ChatRoomId properties somewhere in your view-model, and if so, I’d recommend binding both CurrentYear and both ChatRoomId dependency properties to data in your view-model.

    3. Bindings between two dependency properties are best used with presentation-layer information. For example, you might use a binding between two dependency properties to ensure two controls are the same width. The width of these controls is presentation-layer data since it isn’t what data you’re presenting, but it’s how you’re presenting it. Your CurrentYear and ChatRoomId properties are the data you’re showing, not how you’re showing it, so they’re not presentation-layer data.

    I would also recommend against giving top-level elements a Name or an x:Name and then using that name in a binding. Admittedly, the only XAML you’ve shown is your ChildWindow, so I don’t know whether your MKSelectMonthUC user-control does the same. Nonetheless, for future reference, and for anyone else reading this answer, I’ll give two reasons why this is a bad idea.

    Suppose we have the following UserControl:

    <UserControl x:Class="XYZ.MyUserControl"
                 ....
                 x:Name="myUc">
        <TextBlock Text="{Binding Path=MyProperty, ElementName=myUc}" />
    </UserControl>
    

    If we then attempt to use this control and give it a different x:Name, such as

    <xyz:MyUserControl x:Name="abc123" />
    

    we end up changing the name of the control from myUc to abc123, and this breaks the binding.

    Furthermore, if we attempt to use two or more of these user controls, for example

    <StackPanel>
        <xyz:MyUserControl />
        <xyz:MyUserControl />
    </StackPanel>
    

    then we get an error about x:Names not being unique, as both MyUserControl elements have an x:Name of myUc.

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

Sidebar

Related Questions

I do have two problems regarding custom controls: I created a custom UserControl public
I have UserControl which has two components public System.Windows.Forms.ComboBox innerComboBox; public System.Windows.Forms.TextBox innerTextBox; and
I have two UserControls (uc1 and uc2) loading into a third UserControl (shell). Shell
I have two ViewModels: public class CommandViewModel { public string DisplayName { get; set;
i have a usercontrol with two public properties public DateTime fromdate { get; set;
I have a very simple composite model made of two classes: Public Class ParentModelVM
I have two classes, which reference the third: class Data1 { public Named Xxx
I have the following construction: public static class Constants { public static class Foo
I have two UserControl classes, RequiredFields and OptionalFields . I wanted to have a
I have usercontrols which inherit a base class which looks like this: BasePage.cs: using

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.