首页 > 解决方案 > 如何在更新 WPF(MVVM)中的值时在组合框中显示正确的数据值?

问题描述

我有 2 张桌子

Scan Table have [ScanId],[Name],[Mode] 
Tags Table have [Name],[Description],[ScanRefId]FOREIGN KEY([ScanRefId])

在插入数据时它工作正常。但是在更新时, 我想在组合框中显示 selectedvalue/storedvalue(外键)。但组合框始终显示绑定数据的第一个值。 我错过了什么?

My XAML code is:





         <ComboBox
                x:Name="cbscanservice"
                Style="{StaticResource MaterialDesignFloatingHintComboBox}"
                ItemsSource="{Binding  Scanvalues}"
                SelectedValue="{Binding Scan}"
                SelectedValuePath="Name"
                DisplayMemberPath="Name"
                FontSize="14"
                Grid.Row="2"
                IsEditable="False"
                materialDesign:HintAssist.Hint="Scan Service"
                Height="40"
                Margin="0,0,0,20"
                VerticalAlignment="Bottom">
             </ComboBox>

     ///UpdateviewModel----code 

        public IEnumerable<DB.model.Scan> Scanvalues
        {
            get { return Getscans(); }
        }

            private IList<DB.model.Scan> Getscans()
            {
               _scann = new List<DB.model.Scan>();
               foreach (var scanname in scanService.GetAll())
               {
                   _scann.Add(scanname);
               }

               return _scann;
            }
 public DB.model.Scan Scan
        {
            get { return _scan; }
            set
            {
                _scan = value;
                OnPropertyChanged("Scan");
            }
        }

标签: c#wpfmvvmdata-binding

解决方案


推荐阅读