我有兩個用戶控件:綁定用戶控件的DependencyProperty到其他用戶控件的DependencyProperty
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);
}
}
}
和
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);
}
}
}
在MKSelectPeriodForChatReportCW的XAML我要綁定它的DependencyProperties到MKSelectMonthUC DependencyProperties這樣的:
<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>
上MKSelectPeriodForChatReportCW屬性做得到值(從他們的綁定),但是,MKSelectMonthUC值沒有。所以請幫我找一個解決方案。謝謝。
您是否期待'GetChatReport(ChatRoomId)'被相關控件的更改觸發? Setters和Getters不被綁定使用,它只使用'SetValue'和'GetValue'。他們只是爲了您的方便。改爲爲該依賴項屬性提供更改處理程序。 –