Usuario
Icommand y viewModel

Pregunta
-
tengo esta clase que hereda de ICommand
public class SavePersonCommand : ICommand { public event EventHandler CanExecuteChanged; public bool CanExecute(object parameter) { return !string.IsNullOrEmpty((string)parameter); } public void Execute(object parameter) { string Hola = "Hola"; Hola = Hola + " " + parameter; } public void RaiseCanExecuteChanged() { CanExecuteChanged?.Invoke(this, new EventArgs()); } }
realmente no hace nada. Solo quiero que entre el execute para ir avanzando
en mi XAML esta este codigo
<TextBlock Name="TxtbName" Text="Nombre:" Grid.Row="0" Grid.Column="0" FontSize="12" Height="30"></TextBlock> <TextBlock Name="Txtbastname" Text="Apellido Paterno:" Grid.Row="1" Grid.Column="0" FontSize="12"></TextBlock> <TextBlock Name="TxtbSecondLastName" Text="Apellido Materno:" Grid.Row="2" Grid.Column="0" FontSize="12"></TextBlock> <TextBlock Name="TxtbFullName" Text="Nombre Completo:" Grid.Row="3" Grid.Column="0" FontSize="12"></TextBlock> <TextBox Name="TxtName" Text="{Binding Path=PersonName, Mode=TwoWay}" Grid.Row="0" Grid.Column="1" ></TextBox> <TextBox Name="TxtLastname" Text="{Binding Path=LastName, Mode=TwoWay}" Grid.Row="1" Grid.Column="1"></TextBox> <TextBox Name="TxtSecondLastName" Text="{Binding Path=SecondLastName, Mode=TwoWay}" Grid.Row="2" Grid.Column="1"></TextBox> <TextBox Name="TxtFullName" Text="{Binding Path=FullName, Mode=OneWay}" Grid.Row="3" Grid.Column="1"></TextBox> <Button Name="BtnAdd" Grid.Column="1" Grid.Row="3" Width="100" TextBlock.TextAlignment="Center" Content="Add" HorizontalAlignment="Right" Command="{Binding Path=SavePersonCommand}" CommandParameter="{Binding Text, ElementName=TxtName}"> </Button>
como pueden ver en el botón BtnAdd usa command y el binding a mi clase SavePersondCommand y como parametro esta el elemento TxtName
aqui esta mi viewModel
public class ViewModelPerson : BaseViewModel { private Person person; private SavePersonCommand savePersonCommand; public ViewModelPerson() { this.person = new Person(); this.savePersonCommand = new SavePersonCommand(); this.person.Name = "Gustavo"; } public string PersonName { get { return this.person.Name; } set { this.person.Name = value; base.NotifyPropertyChanged("PersonName"); } } public string LastName { get { return this.person.LastName; } set { this.person.LastName = value; base.NotifyPropertyChanged("LastName"); } } public string SecondLastName { get { return this.person.SecondLastName; } set { this.person.SecondLastName = value; base.NotifyPropertyChanged("SecondLastName"); } } public string FullName { get { return this.GetFullName(this.PersonName, this.LastName, this.SecondLastName); } } public ICommand SavePersonCommand { get { return this.savePersonCommand; } } public ObservableCollection<Person> Persons { get; set; } private string GetFullName(string name, string lastName, string secondLastName) { string fullName = name + " " + lastName + " " + secondLastName; return fullName; } }
El viewModel si esta enlazado.
Por que cuando cambia el texto del TxtName no entra mi metodo Execute
Todas las respuestas
-
hola
>>tengo esta clase que hereda de ICommand
las interfaces no se heredan se implementan
>>realmente no hace nada. Solo quiero que entre el execute para ir avanzando
no usas un framework de MVVM ? lo pregunto porque si lo usaras estos traen una implementacion basica del RelayCommand
analiza como implementa el command
Podrias definir el tuyo propio
Basic MVVM and ICommand Usage Example
el RelayCommand es generico ya que asignas Action<> para indicar la funcion que ejecutara en el ViewModel
saludos
Leandro Tuttini
Blog
MVP Profile
Buenos Aires
Argentina- Propuesto como respuesta Carlos_Ruiz_M jueves, 28 de febrero de 2019 15:52
-