Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
HTML
<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:
C#
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
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
   
    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.
Posted

The error is pointing out that you can only have a OneWay binding from the Source for fields that are auto-populated by the server, as in the Auto-Increment ID field in this case.
 
Share this answer
 
Comments
[no name] 30-Mar-15 11:55am    
I know. But how to fix it?
Mark Farmiloe 30-Mar-15 12:21pm    
Change
Text="{Binding Path=OrderID,Mode=TwoWay}
to
Text="{Binding Path=OrderID,Mode=OneWay}
[no name] 30-Mar-15 12:46pm    
It is not working. Original there was no Mode. I modified it. However whichever the value is, just not working.
Mark Farmiloe 30-Mar-15 15:12pm    
Is the error the same when using Mode=OneWay?
[no name] 30-Mar-15 16:45pm    
Yes. Same error.
I removed all Mode then it works.
C#
<textbox name="txtOrderID" text="{Binding Path=OrderID,Mode=OneWay}" 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}}" 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}}" margin="5,0,10,0" width="75" />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900