Delete textbox wpf MVVM

0

I am trying to delete the text from the textbox after inserting a data in the database. I do everything right except the deletion of the textbox.

        <TextBlock Text="Blog" Grid.Column="1" Grid.Row="1" Margin="0,0,10,0" ></TextBlock>
        <TextBox Grid.Column="2" Grid.Row="1" Margin="0,0,0,20" Width="150" Height="30" VerticalContentAlignment="Center" Text="{Binding Blog.Nombre, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></TextBox>
        <Button Grid.Column="2" Grid.Row="2" Content="Button" HorizontalAlignment="Center"  Width="auto" Command="{Binding CrearBlogCommand}"/>

    public class CrearBlogViewModel : ViewModelBase
    {
        private RelayCommand crearBlogCommand;
        public ICommand CrearBlogCommand
        {
            get
            {
                if(crearBlogCommand == null)
                {
                    this.crearBlogCommand = new RelayCommand(param => CrearBlog(), param => true);
                }
                return crearBlogCommand;
            }         
        }

        private Blog blog;

        public Blog Blog
        {
            get
            {
                if(this.blog == null)
                {
                    this.blog = new Blog();
                }
                return blog;
            }
            set
            {
                
                base.OnPropertyChanged("Blog");
            }
        }

        private void CrearBlog()
        {
            var nuevoBlog = new DABlog();

            try
            {
                nuevoBlog.CrearBLog(new Blog() { Nombre = this.Blog.Nombre });
                MessageBox.Show("Blog creado correctamente");
                this.Blog.Nombre = string.Empty;
            }
            catch (Exception)
            {

                MessageBox.Show("Se a producido un error");
            }            
        }
    }
    
asked by alejandro Martinez Faci 14.03.2018 в 10:44
source

2 answers

1

Even if you have already solved your problem, let me tell you that you could do a better implementation of the MVVM pattern.

You should not be calling PropertyChanged, imagine if you later have more properties in the same case?

It occurs to me that a better solution is that in your Blog class you also implement INotifyPropertyChanged, that way you just write:

this.Blog.Nombre = string.Empty.

And that should update the view.

If you do not want to implement INPC in Blog, the appropriate solution, or at least as the pattern indicates, is to create a proxy of the properties of the model you want to bin.

Example:

private Blog Blog { get; set; }


public string Nombre
{
    get { return Blog.Nombre; }
    set 
    {
        Blog.Nombre = value;
        base.OnPropertyChanged(nameof(Nombre));
    }
}

And instead of

Text="{Binding Blog.Nombre

You do:

Text="{Binding Nombre

In this way you are not only following the pattern but you are not giving work to the model of something that is the responsibility of the ViewModel. And it also avoids problems in the future.

Finally, a few more comments, do not make calls to MessageBox.Show or anything related to the view in the viewmodel, the idea of MVVM is that the ViewModel does not know anything about the view, so you are referring to WPF in the VM.

The other comment is that you totally avoid this:

base.OnPropertyChanged("Blog");

That can introduce bugs, what if you misspell the name of the property? or what if you refactor the name of the property and forget to change the string you pass to the method "OnPropertyChanged?

If you do not want to make changes to your implementation of the MVVM use at least nameof, that way at least the compiler warns you of errors.

Ideally, if you are using .net 4.5, use the CallerMemberName attribute in the OnPropertyChanged method:

public sub OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    // el código seguiría igual
}

In this way, instead of:

base.OnPropertyChanged("Nombre");

You use it like this:

base.OnPropertyChamged();

The compiler will be responsible for putting the name of the property and there is no problem if in the future you change the name of the property.

I hope your answer helps a little.

    
answered by 02.04.2018 / 06:24
source
0

I answer to myself in case someone asks the same question.

OnPropertyChanged(Blog.Nombre = string.Empty);
    
answered by 14.03.2018 в 11:41