or
<
telerik:RadDataServiceDataSource
x:Name
=
"dataServiceDataSource"
AutoLoad
=
"True"
QueryName
=
"Accounts"
Margin
=
"320,21,320,193"
>
<
telerik:RadDataServiceDataSource.DataServiceContext
>
<
local:MyEntities
/>
</
telerik:RadDataServiceDataSource.DataServiceContext
>
</
telerik:RadDataServiceDataSource
>
AccountDetailsFilter =
new
FilterDescriptor(
"accountID_pk"
, FilterOperator.IsEqualTo, a.accountID);
dataServiceDataSource.FilterDescriptors.Add(AccountDetailsFilter);
public
class
MyNorthwindContext : NorthwindEntities
{
public
MyNorthwindContext() :
base
(
new
Uri(
"http://services.odata.org/Northwind/Northwind.svc"
, UriKind.Absolute)){}
public
DataServiceQuery<Category> SortedCategories
{
get
{
if
((
this
._SortedCategories ==
null
))
{
this
._SortedCategories =
base
.CreateQuery<Category>(
"Categories"
)
.AddQueryOption(
"$orderby"
,
"CategoryID desc"
);
}
return
this
._SortedCategories;
}
}
private
DataServiceQuery<Category> _SortedCategories;
}
<
telerik:RadDataServiceDataSource
Name
=
"categoriesDataSource"
QueryName
=
"SortedCategories"
AutoLoad
=
"True"
>
public
class
ImdbTitlesViewModel : ViewModelBase
{
private
ImdbDataContext _imdbContext;
private
QueryableDataServiceCollectionView<ImdbTitle> _imdbTitlesView;
public
QueryableDataServiceCollectionView<ImdbTitle> ImdbTitlesView
{
get
{
return
_imdbTitlesView; }
}
public
IEnumerable<ImdbTitle> ImdbTitles
{
get
{
return
this
._imdbTitlesView;
}
}
private
ImdbTitle _selectedTitle;
public
ImdbTitle SelectedTitle
{
get
{
return
this
._selectedTitle; }
set
{
if
(
this
._selectedTitle != value)
{
this
._selectedTitle = value;
RaisePropertyChanged(
"SelectedTitle"
);
}
}
}
private
bool
_isBusy;
public
bool
IsBusy
{
get
{
return
this
._isBusy; }
set
{
if
(
this
._isBusy != value)
{
this
._isBusy = value;
RaisePropertyChanged(
"IsBusy"
);
}
}
}
public
RelayCommand Startup {
get
;
set
; }
public
RelayCommand Refresh {
get
{
return
LoadImdbTitles; }
set
{ LoadImdbTitles = value; } }
public
RelayCommand LoadImdbTitles {
get
;
set
; }
public
RelayCommand SaveChanges {
get
;
set
; }
public
RelayCommand RejectChanges {
get
;
set
; }
/// <summary>
/// Initializes a new instance of the ImdbTitlesViewModel class.
/// </summary>
public
ImdbTitlesViewModel()
{
Startup =
new
RelayCommand(OnStartup);
LoadImdbTitles =
new
RelayCommand(OnLoadImdbTitles, CanLoadImdbTitles);
SaveChanges =
new
RelayCommand(OnSaveChanges, CanSaveChanges);
RejectChanges =
new
RelayCommand(OnRejectChanges, CanRejectChanges);
if
(IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
// Code runs "for real": Connect to service, etc...
this
._imdbContext =
new
ImdbDataContext();
this
._imdbTitlesView =
new
QueryableDataServiceCollectionView<ImdbTitle>(
this
._imdbContext,
this
._imdbContext.ImdbTitles);
this
._imdbTitlesView.LoadedData +=
new
EventHandler<Telerik.Windows.Controls.DataServices.LoadedDataEventArgs>(OnImdbTitlesViewLoadedData);
this
._imdbTitlesView.PropertyChanged +=
new
System.ComponentModel.PropertyChangedEventHandler(OnImdbTitlesViewPropertyChanged);
this
._imdbTitlesView.PageSize = 25;
this
._imdbTitlesView.AutoLoad =
true
;
this
._imdbTitlesView.SubmittedChanges +=
new
EventHandler<Telerik.Windows.Controls.DataServices.DataServiceSubmittedChangesEventArgs>(OnImdbTitlesViewSubmittedChanges);
}
}
void
OnImdbTitlesViewSubmittedChanges(
object
sender, Telerik.Windows.Controls.DataServices.DataServiceSubmittedChangesEventArgs e)
{
if
(e.HasError)
{
e.MarkErrorAsHandled();
MessageBox.Show(e.Error.Message,
"Error occurred"
, MessageBoxButton.OK);
}
else
{
MessageBox.Show(
"Changes saved successfully!"
,
"Notification"
, MessageBoxButton.OK);
}
}
void
OnImdbTitlesViewPropertyChanged(
object
sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if
(e.PropertyName.Equals(
"IsBusy"
))
{
this
.IsBusy =
this
._imdbTitlesView.IsBusy;
}
if
(e.PropertyName.Equals(
"HasChanges"
))
{
SaveChanges.RaiseCanExecuteChanged();
RejectChanges.RaiseCanExecuteChanged();
}
}
void
OnImdbTitlesViewLoadedData(
object
sender, Telerik.Windows.Controls.DataServices.LoadedDataEventArgs e)
{
if
(e.HasError)
{
e.MarkErrorAsHandled();
MessageBox.Show(e.Error.Message,
"Error occurred"
, MessageBoxButton.OK);
}
}
private
void
OnStartup()
{
// We dont need to startup because the DataView has the property AutoLoad = true
//GetImdbTitles();
}
private
bool
CanLoadImdbTitles()
{
return
!IsBusy;
}
private
void
OnLoadImdbTitles()
{
// Execute the query.
//this.RejectChanges.Execute(null);
this
._imdbTitlesView.Load(
true
);
//this._imdbTitlesView.Load(true);
}
private
bool
CanSaveChanges()
{
if
(
this
.IsBusy)
return
false
;
if
(
this
._imdbTitlesView.HasChanges)
return
true
;
return
false
;
}
private
void
OnSaveChanges()
{
this
._imdbTitlesView.SubmitChanges();
}
private
void
OnRejectChanges()
{
this
._imdbTitlesView.RejectChanges();
}
private
bool
CanRejectChanges()
{
if
(
this
.IsBusy)
return
false
;
if
(
this
._imdbTitlesView.HasChanges)
return
true
;
else
return
false
;
}
public
override
void
Cleanup()
{
// Clean own resources if needed
base
.Cleanup();
}
}