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.
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.
As HiTechMagic says, the
GetChatReportmethod call in theChatRoomIdsetter inMKSelectMonthUCwon’t get called as often as you might expect. If you want a method to be called every time a dependency property changes, use aPropertyChangedCallback. 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
GetValueand the setter should only contain a call toSetValue.Bindings using
ElementNamecan 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 yourCurrentYearandChatRoomIdproperties somewhere in your view-model, and if so, I’d recommend binding bothCurrentYearand bothChatRoomIddependency properties to data in your view-model.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
CurrentYearandChatRoomIdproperties 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
Nameor anx:Nameand then using that name in a binding. Admittedly, the only XAML you’ve shown is yourChildWindow, so I don’t know whether yourMKSelectMonthUCuser-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:If we then attempt to use this control and give it a different
x:Name, such aswe end up changing the name of the control from
myUctoabc123, and this breaks the binding.Furthermore, if we attempt to use two or more of these user controls, for example
then we get an error about
x:Names not being unique, as bothMyUserControlelements have anx:NameofmyUc.