0

自己記述コントロールのバインディングに問題があります。

   <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <i:PagingWrapPanel PageIndex="{Binding RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type local:PaymentView}}, Path=DataContext.CurrentPage}" Background="{Binding RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type local:PaymentView}}, Path=DataContext.PanelColor}" ItemHeight="64" ItemWidth="128"/>
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>

NormalWrapPanel に基づく PagingWrapPanel は、要素をページングする機能を提供します。BackGround Color へのバインドは問題ありません。ViewModel で BackGround Color を変更すると、コントロールが Color を変更しました。しかし、ViewModel (CurrentPage) で独自に記述した DependencyProperty PageIndex を変更しても、何も行われませんでした。

次のように、グリッドでコントロールを直接使用する場合:

<i:PagingWrapPanel Name="test" ItemHeight="64" ItemWidth="128" PageIndex="{Binding CurrentPage}>
            <Button>Test1</Button>
            <Button>Test2</Button>
            <Button>Test3</Button>
            <Button>Test4</Button>
            <Button>Test5</Button>
            <Button>Test6</Button>
            <Button>Test7</Button>
            <Button>Test8</Button>
            <Button>Test9</Button>
            <Button>Test10</Button>
            <Button>Test11</Button>
            <Button>Test12</Button>
            <Button>Test13</Button>
            <Button>Test14</Button>
            <Button>Test15</Button>
            <Button>Test16</Button>
            <Button>Test17</Button>
            <Button>Test18</Button>
            <Button>Test19</Button>
            <Button>Test20</Button>
            <Button>Test21</Button>
            <Button>Test22</Button>
            <Button>Test23</Button>
            <Button>Test24</Button>
            <Button>Test25</Button>
            <Button>Test26</Button>
            <Button>Test27</Button>

        </i:PagingWrapPanel>

問題はありません。CurrentPage を変更すると、PageIndex の値が変更されました。ただし、テンプレートではバインディングは更新されません。

PagingWrapPanel での定義

 public static readonly DependencyProperty PageIndexProperty;

    public int PageIndex
    {
        get
        {
            return (int)GetValue(PagingWrapPanel.PageIndexProperty);
        }

        set
        {
            SetValue(PagingWrapPanel.PageIndexProperty, value);
        }
    }

  static PagingWrapPanel()
    {
        FrameworkPropertyMetadata md1 = new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, PropertyPageIndexChanged);
        PagingWrapPanel.PageIndexProperty = DependencyProperty.Register("PageIndex", typeof(int), typeof(PagingWrapPanel), md1);                 
    }

およびViewModelでの定義

プライベート int _CurrentPage;

public int CurrentPage
{
    get { return _CurrentPage; }
    set
    {
        if (_CurrentPage == value)
            return;

        _CurrentPage = value;
        RaisePropertyChanged(() => CurrentPage);
    }
}

ViewModel は INotifyPropertyChanged インターフェイスを実装します

間違いはどこですか。

私の悪い英語でごめんなさい。;)

4

0 に答える 0