Wojciech Szabowicz
Wojciech Szabowicz

Reputation: 4198

Pass update parameter in WPF using MVVM

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)?

Upvotes: 0

Views: 498

Answers (3)

Rudra
Rudra

Reputation: 144

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.

Upvotes: 0

mm8
mm8

Reputation: 169320

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.

Upvotes: 1

Tim Rutter
Tim Rutter

Reputation: 4679

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.

Upvotes: 0

Related Questions