0

I am new to mvvm, basicaly its my first attempt. At now I have a WPF window, ado.net to connect to my database, and wcf service to make connection between viewmodel and database. The problem is that I cant add data from my view to database. here some code My wcf methods:

[OperationContract]
    public void ManageOrder(Order order, EntityState state)
    {
        using (var context = new SvLaserEntities())
        {
            context.Attach(order);
            context.ObjectStateManager.ChangeObjectState(order, state);
            context.SaveChanges();
        }

    }
    [OperationContract]
    public void ManageClient(Client client, EntityState state)
    {
        using (var context = new SvLaserEntities())
        {
            context.Attach(client);
            context.ObjectStateManager.ChangeObjectState(client, state);
            context.SaveChanges();
        }

    }

A command for binding to button:

public ICommand AddClient
    {
        get
        {
            if ((addClient == null) && (CurrentClient != null))
            {
                addClient = new RelayCommand(() => this.client.ManageClientAsync(CurrentClient, EntityState.Added));
            }
            return addClient;
        }
    }

And xaml code for one of textboxes:

<TextBox HorizontalAlignment="Left" Height="28" 
                         Margin="469,50,0,0" TextWrapping="Wrap" 
                         VerticalAlignment="Top" Width="160"
                         Text="{Binding CurrentClient.Name, Mode=Default, UpdateSourceTrigger=PropertyChanged}"
                         />

Direct problem is when I am filling the textboxes and clicking to add them, I am catching null reference exception here, at client parameter:

public void ManageClient(Client client, EntityState state)

What am I doing wrong?

4

1 回答 1

1

在您的 xaml 中更改

绑定到 TWOway 中的模式属性,例如

   <TextBox Text="{Binding CurrentClient.Name, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged}" />

请确保在 viewmodel 和 model 中实现了 INotifyPropertyChanged

于 2013-04-21T14:00:39.353 回答