简体   繁体   中英

How to get Value In a ComboBox WPF using MVVM

The next code is some kind of weird, but I have to modify to get the value of a selection, in the XAML; takes ItemsSource from a property in the view model... XAML

<ComboBox Height="23" VerticalAlignment="Center" Grid.Row="5" Grid.Column="2" 
                  ItemsSource="{Binding Path=ReglaMateABC, ValidatesOnNotifyDataErrors=False}" 
                  SelectedItem="{Binding Path=Contribucion.ReglaMatematicaTexto, Mode=TwoWay, NotifyOnValidationError=True}" />

in MVVM code:

    public List<String> ReglaMateABC
    {
        get
        {
            List<String> data = new List<string>();
            data.Add(ControlesResource.TextoReglaMatematicaAB);
            data.Add(ControlesResource.TextoReglaMatematicaC);   
            data.Add(ControlesResource.TextoReglaMatematicaD);             
            return data;
        }
    // this set is added for me
        set
        {
              **SomeVariable** = value;
              this.RaisePropertyChanged(() => this.ListTipoPpa);
        }

    }

the ControlesResource.TextoReglaMatematicaxx is Taken from a resource file.

what type of variable ( SomeVariable ) should i use?

Prism.ViewModel is being used, and RaisePropertyChanged what it takes to work?

UPDATE this is transfer object class that is binding in the XAML file, in SelectedItem="{Binding Path= Contribucion.ReglaMatematicaTexto ...

    public class ContribucionTO
{
    /// <summary>
    /// Identificador de la Contribución.
    /// </summary>
    public string ContribucionId { get; set; }

    /// <summary>
    /// Vigencia de la contribución.
    /// </summary>
    public VigenciaTO Vigencia { get; set; }

    /// <summary>
    /// Nombre de la contribución.
    /// </summary>
    [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "ContribucionTONombreRequerido", ErrorMessageResourceType = typeof(MensajesValidacionResource))]
    [StringLength(150, MinimumLength = 0, ErrorMessageResourceName = "ContribucionTONombreLongitud", ErrorMessageResourceType = typeof(MensajesValidacionResource))]
    public string Nombre { get; set; }

    /// <summary>
    /// Descripción de la contribución.
    /// </summary>
    [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "ContribucionTODescripcionRequerido", ErrorMessageResourceType = typeof(MensajesValidacionResource))]
    [StringLength(150, MinimumLength = 0, ErrorMessageResourceName = "ContribucionTODescripcionLongituda", ErrorMessageResourceType = typeof(MensajesValidacionResource))]
    public string Descripcion { get; set; }

    /// <summary>
    /// Valor de la tarifa de la contribución.
    /// </summary>        
    public decimal Tarifa { get; set; }

    /// <summary>
    /// Indica si la tarifa de la contribución se actualiza o no.
    /// </summary>
    public bool ActualizaTarifa { get; set; }

    /// <summary>
    /// Indica si la regla matemática a utilizar es A+B (True) o C (False)
    /// </summary>
    public bool ReglaMatematica { get; set; }

    /// <summary>
    /// Indicador base de la contribución.
    /// </summary>
    public IndicadorTO IndicadorBase { get; set; }

    /// <summary>
    /// Convocatorias que se excluyen de la distribución de la contribución.
    /// </summary>
    public List<ConvocatoriaTO> ConvocatoriasExcluidas { get; set; }

}//end ContribucionTO

For the type of variable, you should use a List. But i see that it is pointless to set it as you are calculating and returning the list on the get accessor.

As for the selected value, you have the SelectedItem binding in your xaml code, and you should use the Contribucion.ReglaMaticaTexto property.

Edit: From the discussion in the comments, I understand that you should update your ViewModel class with another property to bind the selection of the ComboBox. You should not bind directly to the Contribucion class.

A suggestion is:

private string selectedMathRule; 
public string SelectedMathRule 
{ 
  get { return this.selectedMathRule; } 
  set { SetProperty(ref this.selectedMathRule, value); } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM