Basically I created a wpf application. I added a WCF Data service to the project. This data service works well in the browser.
However I got an exception in my WPF.
System.Windows.Markup.XamlParseException occurred
_HResult=-2146233087
_message=A TwoWay or OneWayToSource binding cannot work on the read-only property 'OrderID' of type '<>f__AnonymousType0`3[System.Int32,System.Nullable`1[System.DateTime],System.Nullable`1[System.DateTime]]'.
HResult=-2146233087
IsTransient=false
Message=A TwoWay or OneWayToSource binding cannot work on the read-only property 'OrderID' of type '<>f__AnonymousType0`3[System.Int32,System.Nullable`1[System.DateTime],System.Nullable`1[System.DateTime]]'
The code is from a MCTS EXAM book. The xaml part:
<Window x:Class="OrderEntryProjectWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Menu>
<MenuItem Header="Save" Name="mnuSave" Click="mnuSave_Click" />
<MenuItem Header="New Order" Name="mnuOrder" Click="mnuOrder_Click" />
<MenuItem Header="Exit" Name="mnuExit" Click="mnuExit_Click" />
</Menu>
<ComboBox Grid.Row="1" Name="cmbCustomers" Margin="5" SelectionChanged="cmbCustomers_SelectionChanged" />
<ListBox Grid.Row="2" Margin="5" Name="lstOrders" >
<ListBox.ItemTemplate>
<DataTemplate>
<Border CornerRadius="5" BorderThickness="2"
BorderBrush="Blue" Margin="3">
<StackPanel Orientation="Horizontal">
<TextBox Text="Order #" TextAlignment="Right" Width="40"/>
<TextBox Name="txtOrderID" Text="{Binding Path=OrderID,Mode=TwoWay}" Margin="5,0,10,0" Width="30"/>
<TextBlock Text="Order Date:" TextAlignment="Right" Width="80"/>
<TextBlock Name="txtOrderDate" Text="{Binding Path=OrderDate,StringFormat={}{0:MM/dd/yyyy}, Mode=TwoWay}" Margin="5,0,10,0" Width="75"/>
<TextBlock Text="Required Date:" TextAlignment="Right" Width="80"/>
<TextBlock Name="txtRequiredDate" Text="{Binding Path=RequiredDate,StringFormat={}{0:MM/dd/yyyy}, Mode=TwoWay}" Margin="5,0,10,0" Width="75"/>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
The code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OrderEntryProjectWPF.NorthwindServiceReference;
using System.Data.Services.Client;
namespace OrderEntryProjectWPF
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private NorthwindEntities ctx=new NorthwindEntities(new Uri("http://localhost:53926/NorthwindDataService.svc"));
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Application.Current.Properties["ctx"]=ctx;
var source=new DataServiceCollection<Customer>(ctx.Customers);
cmbCustomers.ItemsSource = source;
cmbCustomers.DisplayMemberPath = "CompanyName";
}
private void mnuSave_Click(object sender, RoutedEventArgs e)
{
}
private void mnuOrder_Click(object sender, RoutedEventArgs e)
{
}
private void mnuExit_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void cmbCustomers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var customer = (Customer)cmbCustomers.SelectedValue;
if (customer == null) return;
lstOrders.ItemsSource =
from o in ctx.Orders
where o.CustomerID == customer.CustomerID
select new
{
o.OrderID,
o.OrderDate,
o.RequiredDate
};
}
}
}
I can't figure it out. I have two-way binding already. Why?
I gave you a snapshot
here.