【问题标题】:How to Make C# WPF ComboBox ItemsSource Works with CodeBehind and MVVM IDataErrorInfo如何使 C# WPF ComboBox ItemsSource 与 CodeBehind 和 MVVM IDataErrorInfo 一起使用
【发布时间】:2017-06-09 18:36:27
【问题描述】:

我在 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}" />

然后在后面的代码中: 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;
}

【问题讨论】:

    标签: c# wpf xaml mvvm combobox


    【解决方案1】:

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

    你能看到这个用于 EditClient.ClientGender 概念的 Repo 吗:): 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) 视图模型....

     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");
                    }
                }
            }
        }
    

    希望对你有帮助!!!

    【讨论】:

    • Taank you for comment ... EditClient.ClientGender 属性我从这个 Repo 中得到这个概念:github.com/SavchenkoDmitry/MVVMBookCRUD
    • 您能否在您的问题中将该属性添加到您的“视图模型”中?这样我就可以准确地为您提供解决方案。
    猜你喜欢
    • 1970-01-01
    • 2015-11-08
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 2014-03-08
    • 2020-07-02
    相关资源
    最近更新 更多