TwoWay或OneWayToSource绑定不能...... [英] A TwoWay or OneWayToSource binding cannot...

查看:184
本文介绍了TwoWay或OneWayToSource绑定不能......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我创建了一个wpf应用程序。我在项目中添加了WCF数据服务。这个数据服务在浏览器中运行良好。



但是我的WPF出现异常。

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]]'



代码来自MCTS EXAM一书。 xaml部分:


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
{
    /// <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
                };
        }
    }
}



我无法理解。我已经有双向绑定了。为什么?

我给你一张快照这里。

推荐答案

错误指出您只能从服务器自动填充的字段的源中获取OneWay绑定,如在这种情况下自动递增ID字段。
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.


我删除了所有模式然后它工作。

I removed all Mode then it works.
<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" />


这篇关于TwoWay或OneWayToSource绑定不能......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆