对不起,我在第一次提问堆栈溢出问题,所以可能是我做错了一些事情。如何使用WPF绑定ComboBox并获取SelectedItem?
我已经开始学习WPF MVVM了,我被卡住了。
我在数据库中有一个参数主表,它有parameterid
,parametername
,parametertype
列。它填充了主数据,例如,国家主人有parametertype
“国家”,城市有“城市”parametertype
,但parameterid
将是唯一的。
现在我有一个客户页面,其中在customerviewmodel对象中我有countryId
和CityId
参数要保存在客户对象中。
我如何结合这
parameterId
直接与customer.CountryId
从XAML?或者这是不可能的?目前我已经达到它如下:定义了一个
CountrySelectedItem
属性并用comboboxSelectedItem
绑定它。当它的属性发生变化时,当任何人改变它的值,然后在viewmodel中,我设置了CountrySelectedItem
set属性中的CustomerCountryId
。如何验证客户的
countryID
财产?在组合框中,我绑定了参数主对象,因此无法将数据注释的必需属性写入参数主实体。
下面是完整的方案
公共类客户 {
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 ?
感谢您提供宝贵的建议和帮助,但我更新了我的问题,只是添加了方案,请让我知道是否可以 –