2012-05-31 55 views
1

我有兩個用戶控件:綁定用戶控件的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值沒有。所以請幫我找一個解決方案。謝謝。

+0

您是否期待'GetChatReport(ChatRoomId)'被相關控件的更改觸發? Setters和Getters不被綁定使用,它只使用'SetValue'和'GetValue'。他們只是爲了您的方便。改爲爲該依賴項屬性提供更改處理程序。 –

回答

0

我沒有所有的源代碼,我不能重現你的問題,所以我只能根據你上面介紹的代碼風格提出一些建議。

  1. 由於HiTechMagic說,在MKSelectMonthUCChatRoomId二傳手的GetChatReport方法調用不會得到儘可能多你所期望的調用。如果您想在每次依賴項屬性更改時調用方法,請使用PropertyChangedCallbackThis page on MSDN有一個如何使用PropertyChangedCallback的例子。

    對於由依賴屬性支持的屬性,getter應該只包含對GetValue的調用,並且setter只應包含對SetValue的調用。

  2. 使用ElementName的綁定可能會很難處理,因爲如果出現問題(例如沒有找到具有給定名稱的元素),它們將保持沉默。假設你的視圖模型中有CurrentYearChatRoomId屬性的值,如果是這樣,我建議將CurrentYearChatRoomId依賴項屬性都綁定到視圖模型中的數據。

  3. 兩個依賴屬性之間的綁定最適用於表示層信息。例如,您可以使用兩個依賴項屬性之間的綁定來確保兩個控件具有相同的寬度。這些控件的寬度是表示層數據,因爲它不是什麼您正在呈現的數據,但是它是如何您正在呈現它。您的CurrentYearChatRoomId屬性是您顯示的數據,而不是您展示的數據,因此它們不是表示層數據。

我也建議反對給頂級元素Namex: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元素有myUcx:Name

相關問題