[英]Binding UserControl DependencyProperty to other UserControl DependencyProperty
我有兩個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);
}
}
}
和
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上的屬性卻無法獲取。 因此,請幫助我找到解決方案。 謝謝。
我沒有您所有的源代碼,也無法重現您的問題,所以我所能做的就是根據您上面介紹的代碼樣式提出一些建議。
作為HiTechMagic稱, GetChatReport
在方法調用ChatRoomId
在二傳手MKSelectMonthUC
不會得到盡可能多你所期望的調用。 如果希望每次依賴項屬性更改時都調用一個方法,請使用PropertyChangedCallback
。 MSDN上的此頁面提供了有關如何使用PropertyChangedCallback的示例。
對於由依賴項屬性支持的屬性,getter僅應包含對GetValue
的調用,而setter應僅包含對SetValue
的調用。
使用ElementName
進行綁定可能很麻煩,因為如果出現問題(例如,未找到具有給定名稱的元素),它們將保持沉默。 假定您在視圖模型中的某處具有CurrentYear
和ChatRoomId
屬性的值,如果是這樣,我建議將CurrentYear
和ChatRoomId
依賴項屬性都綁定到視圖模型中的數據。
兩個依賴項屬性之間的綁定最好與表示層信息一起使用。 例如,您可以在兩個依賴項屬性之間使用綁定,以確保兩個控件的寬度相同。 這些控件的寬度為表現層的數據,因為它是不是你提出什么樣的數據,但它是你如何呈現它。 CurrentYear
和ChatRoomId
屬性是顯示的數據,而不是顯示方式,因此它們不是表示層數據。
我也建議不要給頂級元素一個Name
或一個x:Name
,然后在綁定中使用該名稱。 誠然,您顯示的唯一XAML是ChildWindow
,所以我不知道您的MKSelectMonthUC
用戶控件是否執行相同的操作。 盡管如此,為了將來參考,以及對於其他閱讀此答案的人,我將給出兩個理由說明為什么這不是一個好主意。
假設我們有以下UserControl
:
<UserControl x:Class="XYZ.MyUserControl"
....
x:Name="myUc">
<TextBlock Text="{Binding Path=MyProperty, ElementName=myUc}" />
</UserControl>
如果我們隨后嘗試使用此控件並給它一個不同的x:Name
,例如
<xyz:MyUserControl x:Name="abc123" />
我們最終將控件的名稱從myUc
為abc123
,這破壞了綁定。
此外,例如,如果我們嘗試使用兩個或多個這些用戶控件,
<StackPanel>
<xyz:MyUserControl />
<xyz:MyUserControl />
</StackPanel>
那么我們會收到關於x:Name
不是唯一的錯誤,因為兩個MyUserControl
元素的x:Name
都是myUc
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.