troubleshooting Question

ComboBox selected value using MVVM not showing in the display

Avatar of countrymeister
countrymeister asked on
.NET ProgrammingC#
3 Comments1 Solution1059 ViewsLast Modified:
I have a ComboBox displaying a checkbox and some text.
The combox is bound to a class called SecurityType which has two properties. SecType and IsSelected.

When I select one or more rows I need to get the value of the items selected in the combobox display, instead I see the class bound to the combobox.
The user can select one or more items.

Here is my code


   Model
       public class SecurityType : NotificationObject
    {
     
        public SecurityType(string secType, bool isSelected)
        {
            SecType = secType;          
            IsSelected = isSelected;
         
         
        }
 

        private string _secType;
        public string SecType
        {
            get { return _secType; }
            set
            {
                if (_secType != value)
                {
                    _secType = value;
                    RaisePropertyChanged(() => SecType);
                }
            }
        }

     

        private bool _isSelected;
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (_isSelected != value)
                {
                    _isSelected = value;
                    RaisePropertyChanged(() => IsSelected);
                }
            }
        }

    }
   
   
   
   ViewModel
      public class SecurityViewModel : BaseViewModel
    {
        private ObservableCollection<SecurityType> _securitytypeCollection;
        public ObservableCollection<SecurityType> SecurityTypeCollection
        {
            get { return _securitytypeCollection; }
            set
            {
                if (_securitytypeCollection != value)
                {
                    _securitytypeCollection = value;
                    RaisePropertyChanged(() => SecurityTypeCollection);
                }
            }
        }
        SecurityType _selectedSecurityType;
        public SecurityType SelectedSecurityType
        {
            get
            {
                return _selectedSecurityType;
            }
            set
            {
                _selectedSecurityType = value;
                //RaisePropertyChanged(() => SelectedSecurityType);        
               
            }
        }
        public SecurityViewModel()
        {
           GetData();
        }            
      
      XAML
      
      <c1:C1ComboBox Grid.Row="0" Grid.Column="1"  SelectedItem="{Binding SelectedSecurityType, Mode=TwoWay}"  SelectedValuePath="SecType"   ItemsSource="{Binding SecurityTypeCollection}"  >
                                   
                                    <c1:C1ComboBox.ItemTemplate>
                                        <DataTemplate >
                                            <StackPanel Orientation="Horizontal">
                                                <CheckBox IsChecked="{Binding IsSelected}"
                                                        Width="20" />
                                                <TextBlock Text="{Binding SecType}"
                                                         Width="100" />
                                            </StackPanel>
                                        </DataTemplate>
                                    </c1:C1ComboBox.ItemTemplate>
                                </c1:C1ComboBox>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros