[英]WPF - MVVM - C# Cannot set SelectedItem of combobox after ItemsSource changes
[英]How to Make C# WPF ComboBox ItemsSource Works with CodeBehind and MVVM IDataErrorInfo
我在XAML ClienWindow.xaml中有這個ComboBox
當我使用Mode=TwoWay
將此ItemsSource綁定到ClientGenderSource后面的代碼時,
其無效(無顯示),但驗證錯誤有效(IDataErrorInfo),
但是,當我使用Mode=OneWay
或Mode=OneWayToSource
顯示男性,女性和驗證Mode=OneWayToSource
其工作原理是什么?
<ComboBox
x:Name="ClientGenderField"
Grid.Row="2"
Grid.Column="1"
Width="320"
HorizontalAlignment="Left"
VerticalAlignment="Center"
materialDesign:HintAssist.Hint="Client Gender"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding ClientGender, Mode=TwoWay, ValidatesOnDataErrors=True,
UpdateSourceTrigger=PropertyChanged,
NotifyOnValidationError=true}"
SelectedItem="{Binding EditClient.ClientGender}"
Style="{StaticResource MaterialDesignFloatingHintComboBox}" />
然后在隱藏的代碼中: ClienWindow.xaml.cs
string[] ClientGenderSource = new string[] { "Male", "Female", "Other" };
ClientGenderField.ItemsSource = ClientGenderSource;
然后在ClientViewModel.cs中,我具有ClientGender屬性:
// ClientGender Property
private string _ClientGender;
public string ClientGender
{
get
{
return _ClientGender;
}
set
{
if (_ClientGender != value)
{
_ClientGender = value;
EditClient.ClientGender = _ClientGender;
NotifyPropertyChanged("ClientGender");
}
}
}
ViewModel CTOR:
public ClientViewModel(Client Client)
{
ClientGender = EditClient.ClientGender;
}
我不知道你綁定的是什么EditClient.ClientGender屬性,所以
你能看到這個用於EditClient.ClientGender Concept的倉庫:): https : //github.com/SavchenkoDmitry/MVVMBookCRUD
我無法運行重新創建您的代碼。 我剛剛創建了一個組合框,其中填充了給定的性別,並且在我的位置可以正常工作。 我沒有在后面的代碼中使用任何代碼。
1)Xaml ..
<UserControl x:Class="GateApplication.Views.Payment"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:GateApplication.Views"
xmlns:vm="clr-namespace:GateApplication.ViewModels"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="710" Margin="10,10,10,10">
<Grid>
<Grid.DataContext>
<vm:PaymentViewModel></vm:PaymentViewModel>
</Grid.DataContext>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<ComboBox
Grid.Row="2"
Grid.Column="1"
Width="320"
HorizontalAlignment="Left"
VerticalAlignment="Center"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding gendermappings,
UpdateSourceTrigger=PropertyChanged,
NotifyOnValidationError=true}"
SelectedItem="{Binding GenderName, Mode=TwoWay}" DisplayMemberPath="GenderName"
/>
</Grid>
</UserControl>
2)ViewModel ...
public class PaymentViewModel : ViewModelBase
{
public PaymentViewModel()
{
LoadClientGender();
}
private ObservableCollection<gender> _gendermappings = new ObservableCollection<gender>();
public ObservableCollection<gender> gendermappings
{
get { return _gendermappings; }
set
{
_gendermappings = value;
OnPropertyChanged("gendermappings");
}
}
private void LoadClientGender()
{
List<gender> genders = new List<gender>();
genders.Add(new gender()
{
GenderName = "Male",
});
genders.Add(new gender()
{
GenderName = "Female",
});
genders.Add(new gender()
{
GenderName = "Other",
});
genders.ForEach(_gendermappings.Add);
}
}
3)型號
public class gender:Base
{
private String _GenderName = String.Empty;
public String GenderName
{
get { return _GenderName; }
set
{
if (_GenderName != value)
{
_GenderName = value;
OnPropertyChanged("GenderName");
}
}
}
}
希望對您有幫助!!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.