Avatar of countrymeister
countrymeister

asked on 

ComboBox selected value using MVVM not showing in the display

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>
C#.NET Programming

Avatar of undefined
Last Comment
countrymeister
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

no points: I put your code into code blocks User generated image

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);
                }
            }
        }

    }

Open in new window


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();
        }    

Open in new window

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> 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of countrymeister
countrymeister

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of countrymeister
countrymeister

ASKER

No one looked into this issue. Very disappointed with th experts.
I eventually found a way to do this
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo