0

I am having a problem updating Entity while using WPF MVVM and commands.

My WPF looks like:

    <Popup Margin="10,10,0,13" Name="UpdatePopup" HorizontalAlignment="Left" VerticalAlignment="Top" Width="450" Height="100" IsOpen="{Binding IsOpen, Mode=TwoWay}">
        <Border Padding="5" Background="WhiteSmoke">
            <StackPanel Orientation="Horizontal" DataContext="{Binding CommendationEntity}" Width="450" Height="100">
                <Label Content="Nazwa żódła" Margin="10,10,10,10" Width="75" Height="30"/>
                <TextBox Text="{Binding Name}" Margin="10,10,10,10" Width="130" Height="30" x:Name="Name" />
                <Button Command="{Binding DataContext.UpdateCommand, ElementName=UpdatePopup}" CommandParameter="{Binding id}" Content="Update" Margin="10,10,10,10" Width="80" Height="30"/>
                <Button Command="{Binding DataContext.CancelCommand, ElementName=UpdatePopup}" Content="Anuluj" Margin="10,10,10,10" Width="80" Height="30"/>
            </StackPanel>
        </Border>
    </Popup>

Now to update record I need id and new name so I am passing id with button binding, and I am having trouble passing name, my update method looks like:

    public void UpdateEntity(object obj)
    {
        this.CommendationEntity = this._catalog.Commendations.Single(entity => entity.id == (int)obj);
        this.CommendationEntity.Name = this.Name;

        this._catalog.Commendations.Attach(this.CommendationEntity);
        this._catalog.Entry(this.CommendationEntity).State = System.Data.Entity.EntityState.Modified;
        this._catalog.SaveChanges();
    }

And in the view model I have a property named:

    public string Name
    {
        get { return _name; }
        set
        {
            if (_name == value)
            {
                return;
            }
            this._name = value;
            RaisePropertyChanged("Name");
        }
    }

But when I click Update id is passed as (object obj), with is right, but Name property does not update, what could be wrong?

Maybe its data context (DataContext="{Binding CommendationEntity}") as model name and view model property has same name? I'm new WPF so I can be wrong.

Or maybe there is a way to just click button and whole object will be passed as (object obj)?

Wojciech Szabowicz
  • 3,646
  • 5
  • 43
  • 87
  • have you done some debugging? – wake-0 Jan 13 '17 at 09:42
  • 1
    The Name Property should raise the NotifyPropertyChanged event when it changes. Make sure the ViewModel implements INotifyPropertyChanged. http://stackoverflow.com/questions/6789236/how-does-wpf-inotifypropertychanged-work – Emond Jan 13 '17 at 09:42
  • 1
    If you want to use `CommendationEntity` change CommandParameter to `CommandParameter={Binding}` bc you have `DataContext="{Binding CommendationEntity}"`. As well try to reload data after `SaveChanges`. – Shakra Jan 13 '17 at 10:09

3 Answers3

1

this.Name indicates that the Name property belongs to the same class as the UpdateCommand property and then the path of the binding to the Name property should be set up the same way:

<TextBox Text="{Binding DataContext.Name, ElementName=UpdatePopup}" Margin="10,10,10,10" Width="130" Height="30" x:Name="Name" />

Otherwise you are binding to some other Name property or the binding simply fails.

mm8
  • 163,881
  • 10
  • 57
  • 88
0

Two possibilities:

  1. Is the binding for Name correct? For the commands you are using DataContext.UpdateCommand, ElementName=UpdatePopup so it may be you need to do this as well. If the binding fails you don't get an exception but you should get a message in the output window of VS - worth checking there.
  2. If for some reason in the Name setter: if (_name == value) (perhaps both are emptry strings?) is true then the name won't update.
Tim Rutter
  • 4,549
  • 3
  • 23
  • 47
0

As per you code you are trying to bind Name property of CommendationEntity with textbox control.It is not getting updated because you have not implemented NotifyPropertyChanged for this.CommendationEntity.Name property. You need to implement INotifyProperty at CommendationEntity to reflect the property changed of CommendationEntity class.

Rudra
  • 148
  • 1
  • 10