2011-05-13 107 views
0

我只是做在Silverlight中,簡單的例子,從數據庫中檢索數據,還可以插入,更新和刪除Silverlight中如何重新綁定的DataGrid

我用的子窗口的插入命令,當我點擊「確定」按鈕,在此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();    
    } 

} 

這是子窗口

<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> 

這是我的視圖模型

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(); 
    } 
} 

電網和childwindow看起來像this

回答

1

作爲一個簡單的基本的解決方案,我會做到這一點:

  • 變化你的InsertCustomer Web服務調用返回剛剛保存的已更新的Customer對象。這樣你將得到一個數據對象的更新副本,並且包含任何鍵/ ID。這樣做是一種合理有效的方法,因爲您正在撥打電話並訪問數據庫,在一次可以完成兩個呼叫時沒有意義。

  • 一旦您更改了您的web服務合約並重新生成了您的客戶端代理,InsertCustomerCompletedEventArgs Result屬性應包含更新的Customer對象。如果您現在將此數據對象添加到您的網格中,它將自動顯示在您的網格中(因爲PagedCollectionView實現INotifyCollectionChanged,所以DataGrid綁定將立即拾取它,但請注意分頁可能意味着它在列表中不可見你目前正在看)。

+0

很好 這是我服務 [鏈接](http://img16.imageshack.us/img16/4093/unledzl.jpg) 你會勸我,我不明白? – user746499 2011-05-13 12:59:46