如何重新绑定silverlight datagrid

时间:2011-05-13 12:24:56

标签: asp.net silverlight silverlight-4.0

我只是在Silverlight中做一个简单的例子,它从数据库中检索数据,也可以插入,更新和删除

我使用子窗口进行插入命令,当我点击这个ChildWindow上的“确定”按钮它在数据库中插入而不是在页面上渲染(Silverlight内容)时,因此在数据库中有相同的记录真正插入信息。只有在再次重新午餐后,它才能正确显示(从服务器检索所有数据)

我会发布我的来源

这是Customers.xaml文件

<UserControl x:Class="Store.Customers"
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:Store.ViewModel"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="1000">

<UserControl.Resources>
    <mv:ViewModel x:Key="ViewModel"/>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="127*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="130*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="91*" />
        <RowDefinition Height="99*" />
        <RowDefinition Height="110*" />
    </Grid.RowDefinitions>

    <Button Name="btnEdit" Content="Edit" HorizontalAlignment="Right" Grid.Column="1" Width="55" Height="30"  Margin="0,225,0,0" Click="btnEdit_Click" />
    <data:DataGrid Name="dgCustomer"  
          AutoGenerateColumns="False" VerticalScrollBarVisibility="Visible"
          ItemsSource="{Binding PagedView, Mode=TwoWay, Source={StaticResource ViewModel}}" 
          Grid.Row="1" Grid.Column="1">

        <data:DataGrid.Columns>
            <data:DataGridTextColumn Header="ID" Binding="{Binding CustomerID}"/>
            <data:DataGridTextColumn Header="CompanyName" Binding="{Binding CompanyName}"/>
            <data:DataGridTextColumn Header="ContactName" Binding="{Binding ContactName}"/>
            <data:DataGridTextColumn Header="ContactTitle" Binding="{Binding ContactTitle}"/>
            <data:DataGridTextColumn Header="Address" Binding="{Binding Address}"/>
            <data:DataGridTextColumn Header="City" Binding="{Binding City}"/>
            <data:DataGridTextColumn Header="Region" Binding="{Binding Region}"/>
            <data:DataGridTextColumn Header="PostalCode" Binding="{Binding PostalCode}"/>
            <data:DataGridTextColumn Header="Country" Binding="{Binding Country}"/>
            <data:DataGridTextColumn Header="Phone" Binding="{Binding Phone}"/>
            <data:DataGridTextColumn Header="Fax" Binding="{Binding Fax}"/>
            <data:DataGridCheckBoxColumn Header="IsCitizen" Binding="{Binding IsCitizen}"/>
        </data:DataGrid.Columns>
    </data:DataGrid>
    <data:DataPager HorizontalContentAlignment="Center" x:Name="myPager" 
                        Source="{Binding ItemsSource, ElementName=dgCustomer}" 
                        AutoEllipsis="True"
                        PageSize="10" Grid.Row="2" Grid.Column="1" VerticalAlignment="Top"/>
</Grid>

和此代码

public partial class Customers : UserControl
{
    public Customers()
    {
        InitializeComponent();
    }

    private void btnEdit_Click(object sender, RoutedEventArgs e)
    {
        new AddNewCustomer().Show();            
    }

}

这是childwindow

<controls:ChildWindow x:Class="Store.AddNewCustomer"
       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:mv="clr-namespace:Store.ViewModel"
       Width="450" Height="350" 
       Title="AddNewCustomer" >

<controls:ChildWindow.Resources>
    <mv:ViewModel x:Key="ViewModel"/>
</controls:ChildWindow.Resources>

<Grid x:Name="LayoutRoot" Margin="2">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid>
        <Grid.RowDefinitions >
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions >
            <ColumnDefinition Width="30*"></ColumnDefinition>
            <ColumnDefinition Width="70*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="1" Grid.Column="0" Text="Customer ID :" VerticalAlignment="Center" Margin="2,0,0,0" />
        <TextBox Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" Margin="2,0" x:Name="txtCustomerID" 
                 Text="{Binding CustomerID, Mode=TwoWay, Source={StaticResource ViewModel}}" />

        <TextBlock Grid.Row="2" Grid.Column="0" Text="Company Name :" VerticalAlignment="Center" Margin="2,0,0,0" />
        <TextBox Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" Margin="2,0" x:Name="txtCompanyName" 
                 Text="{Binding CompanyName, Mode=TwoWay, Source={StaticResource ViewModel}}"/>

        <TextBlock Grid.Row="3" Grid.Column="0" Text="Contact Name :" VerticalAlignment="Center" Margin="2,0,0,0" />
        <TextBox Grid.Row="3" Grid.Column="1" VerticalAlignment="Center" Margin="2,0" x:Name="txtContactName" />

        <TextBlock Grid.Row="4" Grid.Column="0" Text="Contact Title :" VerticalAlignment="Center" Margin="2,0,0,0" />
        <TextBox Grid.Row="4" Grid.Column="1" VerticalAlignment="Center" Margin="2,0" x:Name="txtContactTitle" />

        <TextBlock Grid.Row="5" Grid.Column="0" Text="Address :" VerticalAlignment="Center" Margin="2,0,0,0" />
        <TextBox Grid.Row="5" Grid.Column="1" VerticalAlignment="Center" Margin="2,0" x:Name="txtAddressTitle" />

        <TextBlock Grid.Row="6" Grid.Column="0" Text="City :" VerticalAlignment="Center" Margin="2,0,0,0" />
        <TextBox Grid.Row="6" Grid.Column="1" VerticalAlignment="Center" Margin="2,0" x:Name="txtCity" />

        <TextBlock Grid.Row="7" Grid.Column="0" Text="Country :" VerticalAlignment="Center" Margin="2,0,0,0" />
        <TextBox Grid.Row="7" Grid.Column="1" VerticalAlignment="Center" Margin="2,0" x:Name="txtCountry" />
    </Grid>

    <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
    <Button x:Name="OKButton" Content="OK"  Width="75" Height="23" HorizontalAlignment="Right" Click="OKButton_Click"
            Margin="0,12,79,0" Grid.Row="1" Command="{ Binding AddNewCustomer, Mode=TwoWay, Source={StaticResource ViewModel} }"/>
</Grid>

这是我的ViewModel

public class ViewModel : BaseViewModel
{

    #region Fields

    public ObservableCollection<Customer> _items;
    public PagedCollectionView _view;
    public string _customerID;
    public string _companyName;

    #endregion

    #region Constructors

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

    #endregion

    #region Properties

    public ICommand AddNewCustomer { get { return new AddNewCustomerInfo(this); } }

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

    public PagedCollectionView PagedView
    {
        get { return this._view; }
        set
        {
            this._view = value;
            this.OnPropertyChanged("PagedView");
        }
    }

    public string CustomerID
    {
        get { return this._customerID;}
        set
        {
            this._customerID = value;
            this.OnPropertyChanged("CustomerID");
        }
    }

    public string CompanyName
    {
        get { return this._companyName; }
        set
        {
            this._companyName = value;
            this.OnPropertyChanged("CompanyName");
        }
    }

    #endregion

    #region Methods

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

        webService.GetCustomersAsync();
    }

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

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

    }

    public void CreateCustomer()
    {
        DataServiceClient webservice = new DataServiceClient();

        Customer cust = new Customer();
        cust.CustomerID = this.CustomerID;
        cust.CompanyName = this.CompanyName;
        webservice.InsertCustomerCompleted += new EventHandler<InsertCustomerCompletedEventArgs>(webservice_InsertCustomerCompleted);

        webservice.InsertCustomerAsync(cust);

        LoadCustomer();
    }

    void webservice_InsertCustomerCompleted(object sender, InsertCustomerCompletedEventArgs e)
    {
        this.CreateResult = e.Result;
    }

    #endregion


}

public class AddNewCustomerInfo : ICommand
{
    #region Fields

    public ViewModel ViewModel { get; set; }
    #endregion

    #region Constructors

    public AddNewCustomerInfo(ViewModel viewModel)
    {
        this.ViewModel = viewModel;
    }

    #endregion

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
            this.ViewModel.CreateCustomer();
    }
}

网格和子窗口看起来像this

1 个答案:

答案 0 :(得分:1)

作为一个简单的基本解决方案,我会这样做:

  • 更改InsertCustomer网络服务调用,以返回刚刚保存的更新Customer对象。这样您就可以获得数据对象的更新副本,包括任何键/ ID。这样做是一种相当有效的方法,因为你正在进行呼叫并且无论如何都要访问数据库,当它可以在一个中完成时,没有必要进行两次调用。

  • 一旦您更改了Web服务合同并重新生成了客户端代理,InsertCustomerCompletedEventArgs Result属性应包含更新的Customer对象。如果您现在将此数据对象添加到PagedCollectionView,它将自动显示在您的网格中(因为PagedCollectionView实现INotifyCollectionChanged,因此DataGrid绑定将立即将其拾取,但请注意,分页可能意味着它在您目前正在查看的列表中不可见。)