简体   繁体   中英

Getting Value From Combo Box MVVM WPF c#

I'm trying to get the value of a ComboBox in MVVM and pass it to my ViewModel.

My Combo Box in xaml is..

<ComboBox 
   DataContext="{StaticResource WorkorderGroups}" 
   Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" 
   Height="20" 
   ItemsSource="{Binding Path=AllC}" 
   SelectedItem="{Binding Path=SelectedC, Mode=TwoWay}"
/>

Then setting the selected item in WorkorderView

  public List<long?> AllC
    {
        get
        {
            return _workorderRepository.GetCustomersID();
        }
        set
        {
            if (value == _workorderRepository.GetCustomersID())
                return;

            value = _workorderRepository.GetCustomersID();

            base.OnPropertyChanged("AllC");
        }
    }



    public string SelectedC
    {
        get
        {          
                return item;              
        }
        set
        {
                item = value;    
                OnPropertyChanged("SelectedC");
        }
    }

Then on a button click i'm trying to access it in AllWorkorderViewModel, I am using the following code...

    public ICommand Test
    {
        get
        {
            if (_test == null)
            {
                _test = new RelayCommand(
                    param => this.Testy()
                        );
            }
            return _test;
        }
    }

    public void Testy()
    {

            List<string> s = this.AllWorkorders.Select(C => C.SelectedC).ToList();

            string s1 = s[0];

    }

Please ignore method names, this is just draft code.

My problem is the string it returns isn't the item selected, its a List with 200+ items and they are all null. Has anyone got any idea why?

Thanks.

This line I don't understand:

List<string> s = this.AllWorkorders.Select(C => C.SelectedC).ToList();

What is AllWorkorders ? If you want the current selected item, that's just SelectedC by itself. If you want to find an item from an other list you have to do var item = AllWorkorders.FirstOrDefault(x => x.YourMatchingProperty == SelectedC)

I'm not sure that you understand what Select does - I feel like you want to use the Where/First or Single operator: check out these Linq samples: https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

Your ViewModel does not look very correct for me. Here you have a working example with MVVM:

MainWindow XAML :

  <Grid>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <ComboBox Width="100" Height="20" ItemsSource="{Binding AllC}" SelectedItem="{Binding SelectedC, Mode=TwoWay}" Margin="20"/>
        <Button Width="100" Height="20" Margin="20" Command="{Binding GetValueCombobox}"></Button>
        </StackPanel>


    </Grid>

MainWindow.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainViewModel();           

    }  
}

MainWindowViewModel :

 public class MainViewModel : INotifyPropertyChanged
    {
        private readonly List<long?> _workorderRepositoryList = new List<long?>() {1, 2, 5, 3, 4};

    public List<long?> AllC
    {
        get { return _workorderRepositoryList; }
        set
        {
            if (value == _workorderRepositoryList)
                return;

            value = _workorderRepositoryList;
            OnPropertyChanged("AllC");
        }
    }


    private string item;

    public string SelectedC
    {
        get { return item; }
        set
        {
            item = value;
            OnPropertyChanged("SelectedC");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }


    public ICommand EvaluateStateCommand
    {
        get
        {
            return new RelayCommand(parameter =>
            {
                var myItem = SelectedC;
                //your selected item (the value of the combo) is represented by SelectedC

            });
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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