简体   繁体   English

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

[英]How to Make C# WPF ComboBox ItemsSource Works with CodeBehind and MVVM IDataErrorInfo

i have this ComboBox in XAML ClienWindow.xaml 我在XAML ClienWindow.xaml中有这个ComboBox

when i bind this ItemsSource to Code Behind ClientGenderSource , with Mode=TwoWay , 当我使用Mode=TwoWay将此ItemsSource绑定到ClientGenderSource后面的代码时,

its Not Working (nothing to show) But the Validation Error Works (IDataErrorInfo), 其无效(无显示),但验证错误有效(IDataErrorInfo),

But when i use Mode=OneWay or Mode=OneWayToSource its works By showing Male, Female, and Validation Not Working? 但是,当我使用Mode=OneWayMode=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}" />

then in Code Behind : ClienWindow.xaml.cs 然后在隐藏的代码中: ClienWindow.xaml.cs

string[] ClientGenderSource = new string[] { "Male", "Female", "Other" };

ClientGenderField.ItemsSource = ClientGenderSource;

then in ClientViewModel.cs i have ClientGender Property: 然后在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: ViewModel CTOR:

public ClientViewModel(Client Client)
{
ClientGender = EditClient.ClientGender;
}

i dont know what is EditClient.ClientGender property which you are binding , so 我不知道你绑定的是什么EditClient.ClientGender属性,所以

can you see this Repo for EditClient.ClientGender Concept :) : https://github.com/SavchenkoDmitry/MVVMBookCRUD 你能看到这个用于EditClient.ClientGender Concept的仓库:): https : //github.com/SavchenkoDmitry/MVVMBookCRUD

i could not run recreat your code. 我无法运行重新创建您的代码。 I just created one combobox with given genders populated, and its working fine at my place. 我刚刚创建了一个组合框,其中填充了给定的性别,并且在我的位置可以正常工作。 I did not use any code in code behind. 我没有在后面的代码中使用任何代码。

1) Xaml.. 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.... 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) Model 3)型号

 public class gender:Base
    {

        private String _GenderName = String.Empty;
        public String GenderName
        {
            get { return _GenderName; }
            set
            {
                if (_GenderName != value)
                {
                    _GenderName = value;
                    OnPropertyChanged("GenderName");
                }
            }
        }
    }

Hope It help!!! 希望对您有帮助!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM