【发布时间】:2018-01-23 14:35:39
【问题描述】:
很抱歉,我第一次问关于堆栈溢出的问题,所以可能是我做错了什么。
我已经开始学习 WPF MVVM 并且卡住了。
我在数据库中有一个参数主表,它有 parameterid、parametername、parametertype 列。它填充了主数据,例如,国家主数据有parametertype“国家”,城市有“城市”parametertype,但parameterid 将是唯一的。
现在我有一个客户页面,在 customerviewmodel 对象中我有 countryId 和 CityId 参数要保存在客户对象中。
-
如何将这个
parameterId直接与来自XAML 的customer.CountryId绑定?还是不可能?目前我已经实现了如下:定义了一个
CountrySelectedItem属性并将其与组合框SelectedItem绑定。当它的属性改变时,当任何人改变它的值时,我在视图模型中设置客户的CountryIdCountrySelectedItem设置属性。 如何验证客户的
countryID属性?在组合框中,我已经绑定了 Parameter Master 的对象,所以无法将数据 Annotation 的 Required 属性写入 Parameter Master Entity。
这里是完整的场景
公共类客户
{
public int CustomerId { get; set; }
[Required]
public string CustomerName { get; set; }
[Required]
public int CountryId { get; set; }
}
<telerik:RadComboBox x:Name="cmCountryId" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Left"
ItemsSource="{Binding LstCountry}"
HorizontalContentAlignment="Left" VerticalAlignment="Center"
DisplayMemberPath="ParameterName"
SelectedItem="{Binding SelectedCountry,Mode=TwoWay}" Height="25" Width="200" >
</telerik:RadComboBox>
/* To Fill DropDown Country DropDown From ParameterMaster*/
public List<ParameterEntity> LstCountry { get; set; }
LstContainerType = new List<ParameterEntity>(parameterService.GetParamterTypeDetail("COUNTRY").ToList());
/*
Parameter Master has ParameterId,ParameterName,ParameterType -- All Master Data Stored Inside It with Different ParameterType.
*/
/* When User Select CountrId then I Set It's ParameterID As CountryID In My CustomerEntity */
public ParameterEntity SelectedCountry
{
get
{
return _selectedCountry;
}
set
{
_selectedCountry = value;
RaisePropertyChanged("SelectedCountry");
SelectedCustomerEntity.CountryId = _selectedCountry != null ? _selectedCountry.ParameterId : 0;
}
}
So Here Is my question for above Scneraio.
1) To Bind Customer Object's CountryId Property From CountryDropDown is that any other option available then this one.
SelectedCustomerEntity.CountryId = _selectedCountry != null ? _selectedCountry.ParameterId : 0;
// please have a look SelectedCountry property.
Something like , I do not need to write Selected Property and i Can Directly Set ParameterId To CountryId Of CustomerEntity From XAML
Or this one is right scneario.
2) Another Question Is How To do Validation On CountryId ComboBox.
i mean As Mention in CustomerEntity has Required CountryId But In XAMl Design of SelectedItem="{Binding SelectedCountry,Mode=TwoWay}"
What should i write to display if user has not select any country from dropdown. Should I write a logic on Save Button manully ?
【问题讨论】:
标签: wpf validation mvvm data-binding combobox