繁体   English   中英

如何重新绑定Silverlight DataGrid

[英]how rebind silverlight datagrid

我只是在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();            
    }

}

这是子窗口

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

网格和子窗口看起来像这样

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

  • 更改您的InsertCustomer Web服务调用,以返回刚刚保存的更新后的Customer对象。 这样,您将获得数据对象的更新副本,其中包含所有键/ ID。 这样做是一种相当有效的方法,因为无论如何您都在进行调用和访问数据库,一次完成两个调用就没有意义。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM