簡體   English   中英

如何使用 MVVM 在 ComboBox WPF 中獲取值

[英]How to get Value In a ComboBox WPF using MVVM

下一個代碼有點奇怪,但我必須修改以獲取 XAML 中選擇的值; 從視圖模型中的屬性中獲取 ItemsSource ... 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}" />

在 MVVM 代碼中:

    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);
        }

    }

ControlesResource.TextoReglaMatematicaxx取自資源文件。

我應該使用什么類型的變量( SomeVariable )?

正在使用 Prism.ViewModel 和 RaisePropertyChanged 需要什么工作?

更新這是在 XAML 文件中綁定的傳輸對象類,在 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

對於變量類型,您應該使用列表。 但是我看到在計算和返回 get 訪問器上的列表時設置它是沒有意義的。

至於選定的值,您的 xaml 代碼中有 SelectedItem 綁定,您應該使用 Contribucion.ReglaMaticaTexto 屬性。

編輯:從評論中的討論中,我了解到您應該使用另一個屬性更新您的 ViewModel 類以綁定 ComboBox 的選擇。 您不應直接綁定到 Contribucion 類。

一個建議是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM