问题SilverLight 4 DataPager控件… [英] Problem SilverLight 4 DataPager control…

查看:57
本文介绍了问题SilverLight 4 DataPager控件…的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将DataPager绑定到DataGrid

这是xaml

I want to bind DataPager to DataGrid

here is xaml

<<pre lang="xml">UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="NorthWindSilver.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:mv="clr-namespace:NorthWindSilver.ViewModel"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <mv:ViewModel x:Key="ViewModel"/>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <data:DataGrid Name="dgCustomer" AutoGenerateColumns="True" ItemsSource="{Binding Items, Mode=TwoWay, Source={StaticResource ViewModel}}">
        </data:DataGrid>
        <sdk:DataPager HorizontalContentAlignment="Center" x:Name="myPager" Grid.Row="2" Source="{Binding Path=ItemsSource, ElementName=dgCustomer}" PageSize="10"/>
    </Grid>
</UserControl


>

和ViewModel


>

and ViewModel

public class ViewModel :  INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<Customer> _items;

    public ViewModel()
    {
        if (!IsDesignTime)
            this.Customer();
    }

    public void ChangeProperty(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }      

    public ObservableCollection<Customer> Items
    {
        get
        {
            return this._items;
        }
        set
        {
            this._items = value;
            ChangeProperty("Items");
        }
    }


    public bool IsDesignTime
    {
        get
        {
            return (Application.Current == null) ||
                (Application.Current.GetType() == typeof(Application));
        }
    }


    public void Customer()
    {
        DataServiceClient webService = new DataServiceClient();
        webService.GetCustomersCompleted += new EventHandler<GetCustomersCompletedEventArgs>(webService_GetCustomersCompleted);

        webService.GetCustomersAsync();
    }


    void webService_GetCustomersCompleted(object sender, GetCustomersCompletedEventArgs e)
    {
        Items = e.Result;

        PagedCollectionView pageView = new PagedCollectionView(Items);          

        MainPage ma = new MainPage();

        ma.dgCustomer.ItemsSource = pageView;
    }
}





这是结果

如您所见,DataPager无法正常运行
什么问题?





Here is result

As you see DataPager does not work
what the problem?

推荐答案

将代码修改为

1.创建一个属性
Modify your code to

1. Create a property
<pre lang="css">private PagedCollectionView _view;
        public PagedCollectionView PagedView
        {
            get
            {
                return this._view;
            }
            set
            {
                this._view = value;
                ChangeProperty("PagedView");
            }
        }





2.在webService_GetCustomersCompleted处理程序中设置此属性





2. Set this Property in webService_GetCustomersCompleted handler

PagedCollectionView pageView = new PagedCollectionView(((ViewModel)dgCustomer.DataContext).Items);
            pageView.PageSize = 10;
            PagedView = pageView;



3.使用此属性绑定网格



3. Bind your grid using this property

DataContext="{StaticResource ViewModel}" ItemsSource="{Binding Path=PagedView, Mode=TwoWay}"


这篇关于问题SilverLight 4 DataPager控件…的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆