|
|
Demonstrated issues:
Prerequisities:
Steps:
public class Northwind : DataService< /* TODO: put your data source class name here */ >
public class Northwind : DataService<NorthwindEntities>
public class Northwind : DataService<NorthwindEntities>
{
public static void InitializeService(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("Employees", EntitySetRights.AllRead);
config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead);
config.SetEntitySetAccessRule("Orders", EntitySetRights.All);
config.SetEntitySetAccessRule("Order_Details", EntitySetRights.All);
config.SetEntitySetAccessRule("Products", EntitySetRights.All);
}
}
<StackPanel>
<TextBox
x:Name="StatusTextBox"
Margin="5"
Padding="5"
TextWrapping="Wrap"
HorizontalAlignment="Center"
VerticalAlignment="Center"
VerticalScrollBarVisibility="Auto"
/>
<ComboBox
x:Name="CustomersComboBox"
Width="260"
ItemsSource="{Binding}"
/>
</StackPanel>
public partial class MainPage : UserControl
{
private NorthwindEntities dataContext;
public MainPage()
{
InitializeComponent();
dataContext = new NorthwindEntities(new Uri("Northwind.svc", UriKind.Relative));
GetCustomers();
}
private void GetCustomers()
{
ShowWaiting("Retrieving customers...");
DataServiceQuery<Customers> query = dataContext.Customers;
query.BeginExecute(OnCustomersQueryComplete, query);
}
private void OnCustomersQueryComplete(IAsyncResult ar)
{
Dispatcher.BeginInvoke(() =>
{
try
{
var query = (DataServiceQuery<Customers>)ar.AsyncState;
CustomersComboBox.DataContext = query.EndExecute(ar);
ShowSuccess("Customers retrieved");
}
catch (Exception e)
{
ShowError("Error during retrieving customers: " + e);
}
});
}
private void ShowWaiting(string text)
{
ShowStatus(Colors.Yellow, Colors.Black, text);
}
private void ShowSuccess(string text)
{
ShowStatus(Colors.Green, Colors.White, text);
}
private void ShowError(string text)
{
ShowStatus(Colors.Red, Colors.White, text);
}
private void ShowStatus(Color backgroundColor, Color foregroundColor, string text)
{
StatusTextBox.Background = new SolidColorBrush(backgroundColor);
StatusTextBox.Foreground = new SolidColorBrush(foregroundColor);
StatusTextBox.Text = text;
}
}
<ComboBox
x:Name="CustomersComboBox"
Width="260"
ItemsSource="{Binding}"
>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding CustomerID}" Margin="2,0,2,0"/>
<TextBlock Text="{Binding CompanyName}" Margin="2,0,2,0"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<data:DataGrid
x:Name="OrdersDataGrid"
Height="200"
Margin="5"
Padding="5"
AutoGenerateColumns="True"
ItemsSource="{Binding}"
/>
<ComboBox
x:Name="CustomersComboBox"
Width="260"
ItemsSource="{Binding}"
SelectionChanged="CustomersComboBox_SelectionChanged"
>
private void CustomersComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (CustomersComboBox.SelectedItem != null)
{
Customers selectedCustomer = (Customers) CustomersComboBox.SelectedItem;
GetOrders(selectedCustomer);
}
}
private void GetOrders(Customers customer)
{
ShowWaiting("Retrieving orders...");
dataContext.BeginLoadProperty(customer, "Orders", OnOrdersQueryComplete, null);
}
private void OnOrdersQueryComplete(IAsyncResult ar)
{
Dispatcher.BeginInvoke(() =>
{
try
{
OrdersDataGrid.DataContext = dataContext.EndLoadProperty(ar);
ShowSuccess("Orders retrieved");
}
catch (Exception e)
{
ShowError("Error during retrieving orders: " + e);
}
});
}
private void OnOrdersQueryComplete(IAsyncResult ar)
{
Dispatcher.BeginInvoke(() =>
{
try
{
OrdersDataGrid.DataContext = dataContext.EndLoadProperty(ar);
OrdersDataGrid.UpdateLayout();
CollapseDataGridColumn(OrdersDataGrid, "Customers");
CollapseDataGridColumn(OrdersDataGrid, "Employees");
CollapseDataGridColumn(OrdersDataGrid, "Order_Details");
ShowSuccess("Orders retrieved");
}
catch (Exception e)
{
ShowError("Error during retrieving orders: " + e);
}
});
}
private static void CollapseDataGridColumn(DataGrid dataGrid, string columnHeader)
{
var column = dataGrid.Columns.Where(c => (string) c.Header == columnHeader).FirstOrDefault();
if (column != null)
{
column.Visibility = System.Windows.Visibility.Collapsed;
}
}
<data:DataGrid
x:Name="OrdersDataGrid"
Height="200"
Margin="5"
Padding="5"
AutoGenerateColumns="True"
ItemsSource="{Binding}"
RowEditEnded="OrdersDataGrid_RowEditEnded"
/>
<Button
Width="100"
HorizontalAlignment="Left"
Margin="5,0,5,0"
x:Name="SaveButton"
Content="Save changes"
Click="SaveButton_Click"
/>
private void OrdersDataGrid_RowEditEnded(object sender, DataGridRowEditEndedEventArgs e)
{
dataContext.UpdateObject(OrdersDataGrid.SelectedItem);
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
ShowWaiting("Saving changes...");
dataContext.BeginSaveChanges(SaveChangesOptions.Batch, OnChangesSaved, dataContext);
}
private void OnChangesSaved(IAsyncResult ar)
{
Dispatcher.BeginInvoke(() =>
{
dataContext = (NorthwindEntities)ar.AsyncState;
try
{
DataServiceResponse response = dataContext.EndSaveChanges(ar);
ShowSuccess(string.Format(
"Changes saved, status code: {0}", response.BatchStatusCode));
}
catch (Exception e)
{
ShowError("Error during saving changes: " + e);
}
});
}