在组合框中获取空值

问题描述 投票:0回答:0

我需要帮助。我有一个组合框表,效果很好,看起来像这样:

 private Workers _currentWorkers = new Workers();
        public WorkersEditAdd(Workers selectWorkers)
        {
            InitializeComponent();

            if(selectWorkers != null)
                _currentWorkers= selectWorkers;
            DataContext = _currentWorkers;
            ComboFunctions.ItemsSource = Toy_StoreEntities1.GetContext().Functions.ToList();
        }
        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder errors = new StringBuilder();

            if (string.IsNullOrWhiteSpace(_currentWorkers.First_name))
                errors.AppendLine("Please enter First name");
            if (string.IsNullOrWhiteSpace(_currentWorkers.Last_name))
                errors.AppendLine("Please enter Last name");
            if (string.IsNullOrWhiteSpace(_currentWorkers.Third_name))
                errors.AppendLine("Please enter Third name");
            if (_currentWorkers.Salary < 0)
                errors.AppendLine("Please enter a number of Salary more than 0");
            if (_currentWorkers.Key <= 0)
                errors.AppendLine("Please enter a number of Key more than 0");
            if (_currentWorkers.Functions == null)
                errors.AppendLine("Choose a function");

            if (errors.Length > 0)
            {
                MessageBox.Show(errors.ToString());
                return;
            }

组合框行的格式如下:

<ComboBox SelectedItem="{Binding Functions}" Grid.Column="1" Margin="20,10,8,10" x:Name="ComboFunctions" DisplayMemberPath="Function" Grid.Row="4"></ComboBox>

但是当我开始像这样在另一个表中添加组合框时:

public SellsEditAdd(Sells selectSells)
        {
            InitializeComponent();

            if (selectSells != null)
                _currentSells = selectSells;
            DataContext = _currentSells;
            ComboToys.ItemsSource = Toy_StoreEntities1.GetContext().Toys.ToList();
            ComboForms.ItemsSource = Toy_StoreEntities1.GetContext().Forms.ToList();
            ComboCountries.ItemsSource = Toy_StoreEntities1.GetContext().Countries.ToList();
            ComboFirms.ItemsSource = Toy_StoreEntities1.GetContext().Firms.ToList();
            ComboWorkers.ItemsSource = Toy_StoreEntities1.GetContext().Workers.ToList();
            ComboClients.ItemsSource = Toy_StoreEntities1.GetContext().Clients.ToList();

        }

我收到消息“请输入玩具……公司”等

他们的组合框行格式如下:

<ComboBox SelectedItem="{Binding Toy}" Grid.Column="1" Margin="20,10,8,10" x:Name="ComboToys" DisplayMemberPath="Toy" Grid.Row="1"></ComboBox>
        <ComboBox SelectedItem="{Binding Form}" Grid.Column="1" Margin="20,10,8,10" x:Name="ComboForms" DisplayMemberPath="Form" Grid.Row="2"></ComboBox>
        <ComboBox SelectedItem="{Binding Country}" Grid.Column="1" Margin="20,10,8,10" x:Name="ComboCountries" DisplayMemberPath="Country" Grid.Row="4"></ComboBox>
        <ComboBox SelectedItem="{Binding Firm}" Grid.Column="1" Margin="20,10,8,10" x:Name="ComboFirms" DisplayMemberPath="Firm" Grid.Row="5"></ComboBox>

        <ComboBox SelectedItem="{Binding Worker}" Grid.Column="1" Margin="20,10,8,10" x:Name="ComboWorkers" DisplayMemberPath="First_name" Grid.Row="8"></ComboBox>
        <ComboBox SelectedItem="{Binding Client}" Grid.Column="1" Margin="20,10,8,10" x:Name="ComboClients" DisplayMemberPath="First_name" Grid.Row="9"></ComboBox>

red comboboxes

我应该改变什么?为什么第一张桌子工作得很好,但其他桌子却不行?提前谢谢你

c# entity-framework combobox
© www.soinside.com 2019 - 2024. All rights reserved.