1

2つのUserControlがあります。

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を次のようにMKSelectMonthUCDependencyPropertiesにバインドします。

<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の値は取得しません。だから私が解決策を見つけるのを手伝ってください。ありがとう。

4

1 に答える 1

0

私はあなたのすべてのソースコードを持っておらず、あなたの問題を再現することはできないので、私にできることはあなたが上に提示したコードのスタイルに基づいていくつかの提案をすることです。

  1. HiTechMagicが言うように、 setter inのGetChatReportメソッド呼び出しは、期待するほど頻繁には呼び出されません。依存関係プロパティが変更されるたびにメソッドが呼び出されるようにする場合は、を使用します。 MSDNのこのページには、PropertyChangedCallbackの使用方法の例があります。 ChatRoomIdMKSelectMonthUCPropertyChangedCallback

    依存関係プロパティに基づくプロパティの場合、ゲッターにはへの呼び出しGetValueのみが含まれ、セッターにはへの呼び出しのみが含まれる必要がありますSetValue

  2. を使用したバインディングElementNameは、問題が発生した場合にサイレントになるため(たとえば、指定された名前の要素が見つからなかった場合)、操作が面倒になる可能性があります。おそらく、ビューモデルのどこかにプロパティの値があるので、そうであれば、両方と両方の依存関係プロパティをビューモデルのデータにバインドすることをお勧めCurrentYearします。ChatRoomIdCurrentYearChatRoomId

  3. 2つの依存関係プロパティ間のバインドは、プレゼンテーション層の情報で最もよく使用されます。たとえば、2つの依存関係プロパティ間のバインディングを使用して、2つのコントロールが同じ幅になるようにすることができます。これらのコントロールの幅は、表示するデータではないため、プレゼンテーション層のデータですが、表示方法です。CurrentYearプロパティは表示するデータであり、表示方法ChatRoomIdではないため、プレゼンテーション層のデータではありません。

また、最上位の要素にaNameまたはanx:Nameを付けてから、その名前をバインディングで使用することはお勧めしません。確かに、表示したXAMLはあなただけなので、ユーザーコントロールが同じChildWindowことをするかどうかはわかりません。MKSelectMonthUCそれにもかかわらず、将来の参考のために、そしてこの答えを読んでいる他の人のために、これが悪い考えである理由を2つ挙げます。

次のものがあるとしますUserControl

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

次に、このコントロールを使用して、次x:Nameのような別のコントロールを指定しようとすると、

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

コントロールの名前をからに変更することmyUcabc123なり、これによりバインディングが解除されます。

さらに、これらのユーザーコントロールを2つ以上使用しようとすると、たとえば

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

次に、両方の要素にのが含まれているためx:Name、sが一意ではないというエラーが発生します。MyUserControlx:NamemyUc

于 2012-06-03T08:17:58.250 に答える