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>

PagingWrapPanel 基于 NormalWrapPanel 并为我提供了对元素进行分页的功能。绑定到背景颜色没有问题,当我在 ViewModel 中更改背景颜色时,控件更改了颜色。但是,当我在 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