如何使C#WPF ComboBox ItemsSource与CodeBehind和MVVM IDataErrorInfo一起使用

时间:2017-06-09 18:36:27

标签: c# wpf xaml mvvm combobox

我在XAML中有这个ComboBox ClienWindow.xaml

当我将此ItemsSource绑定到 ClientGenderSource 后面的代码时,使用Mode=TwoWay

它不工作(没有显示)但验证错误有效(IDataErrorInfo),

但当我使用Mode=OneWayMode=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}" />

然后在Code Behind中: 的 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;
}

1 个答案:

答案 0 :(得分:0)

我不知道你绑定的EditClient.ClientGender属性是什么,所以

你能看到这个Repo for 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");
                }
            }
        }
    }

希望有所帮助!!!