![](/img/trans.png)
[英]how to set combobox default value in wpf using binding in mvvm when manually setting combobox items
[英]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.