【问题标题】:How to set combo box value in WPF using MVVM architecture如何使用 MVVM 架构在 WPF 中设置组合框值
【发布时间】:2020-05-20 05:45:27
【问题描述】:

我在 WPF MVVM 架构中创建了一个 POC。我在其中使用了组合框控件,您可以在下面的代码中看到。

 <ComboBox Name="DeptCombo" Grid.Row="4" Grid.Column="1" ItemsSource="{Binding DepartmentList,Mode=TwoWay}" SelectedItem="{Binding Path=CurrentDepartment,Mode=TwoWay}" DisplayMemberPath="DepartmentName">
</ComboBox>

这里 CurrentDepartment 是 Department 类的一个属性。

一切都很好,我填充了该组合,将该组合值保存在数据库中,但我面临的唯一问题是,我无法在该组合中设置保存的数据库值。我没有得到任何解决方案关于那个。请帮助我。

【问题讨论】:

    标签: c# mvvm combobox wpf-controls


    【解决方案1】:

    添加UpdateSourceTrigger

      SelectedItem="{Binding Path=CurrentDepartment,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
    

    他的代码应该是这样的:

      class ViewModel: INotifyPropertyChanged
    {
        public ObservableCollection<string> Datas { get; set; } = new ObservableCollection<string>()
        {
            "FF", "AA", "BB"
        };
    
        private string currentItem;
        public string CurrentItem
        {
            get => currentItem;
            set
            {
                currentItem = value;
                OnPropertyChanged("CurrentItem");
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName]string prop="")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
        }
    }
    

    型号示例: PersonModel.cs:

       class PersonModel
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    

    ViewModel.cs:

     class ViewModel: INotifyPropertyChanged
        {
            public ObservableCollection<PersonModel> Datas { get; set; } = new ObservableCollection<PersonModel>()
            {
                new PersonModel(){Age = 10, Name="Tom"},
                new PersonModel(){Age = 10, Name="Mark"},
            };
    
            private PersonModel currentItem;
            public PersonModel CurrentItem
            {
                get => currentItem;
                set
                {
                    currentItem = value;
                    OnPropertyChanged("CurrentItem");
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            public void OnPropertyChanged([CallerMemberName]string prop="")
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
            }
        }
    

    MainWindow.xaml:

     <ComboBox ItemsSource="{Binding Datas}" SelectedItem="{Binding CurrentItem, UpdateSourceTrigger=PropertyChanged}"
                  Height="100" Width="100">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Text="{Binding Age}"/>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    

    【讨论】:

    • 嗨@典型请详细解释您的解决方案为什么我在这里使用 UpdateSourceTrigger
    • @Shubham,当 ViewModel CurrentDepartment 发生变化时,它需要更新您的 ComboBox 属性。 UpdateSourceTrigger 旨在设置更新数据的逻辑。
    • 它不起作用我需要在视图模型中添加更多关于 PropertyChanged 的​​内容
    • @Shubham,我在回复中添加了一个示例
    • 完美@典型,当我在组合中绑定字符串类型变量列表时它工作正常,但是当我使用类类型对象列表时,问题就出现了,在我的情况下 ItemsSource="{Binding DepartmentList,Mode=TwoWay}" 这里Department list是Department类的列表,SelectedItem="{Binding path=CurrentDepartment,Mode=TwoWay}" 这里CurrentDepartment是Department类的属性
    【解决方案2】:

    部门类:

     public class Department : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertychanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    
        private int id;
        public int Id
        {
            get { return id; }
            set { id = value; OnPropertychanged("Id"); }
        }
    
        private string departmentName;
        public string DepartmentName
        {
            get { return departmentName; }
            set { departmentName = value; OnPropertychanged("DepartmentName"); }
        }
    
        private bool isActive;
        public bool IsActive
        {
            get { return isActive; }
            set { isActive = value; OnPropertychanged("IsActive"); }
        }
    }
    

    查看模型:

    private ObservableCollection<Department> departmentList;
        public ObservableCollection<Department> DepartmentList
        {
            get { return departmentList; }
            set { departmentList = value; OnPropertyChanged("DepartmentList"); }
        }
    
     private Department currentDepartment;
        public Department CurrentDepartment
        {
            get { return currentDepartment; }
            set { currentDepartment = value; OnPropertyChanged("CurrentDepartment"); }
        }
    
     private void DepartmentPop()
        {
            DepartmentList = new ObservableCollection<Department> 
                    (objDepartmentService.GetAll());
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 2014-03-30
      相关资源
      最近更新 更多