user2819435
user2819435

Reputation: 1

Accessing combo box value to view model

Helo,i am using mvvm...i have a combobox,here is the xaml code

 <ComboBox Grid.Column="4" Grid.Row="3" Height="23" SelectedValue="{Binding Path=Percentage, Mode=TwoWay}"  SelectedValuePath="Percentage" HorizontalAlignment="Left" Margin="18,7,0,0" Name="comboBox1" VerticalAlignment="Top" Width="144">
                <ComboBoxItem Content="12"></ComboBoxItem>
                <ComboBoxItem Content="13"></ComboBoxItem>
                <ComboBoxItem Content="14"></ComboBoxItem>
                <ComboBoxItem Content="15"></ComboBoxItem>
                <ComboBoxItem Content="16"></ComboBoxItem>
                <ComboBoxItem Content="17"></ComboBoxItem>
                <ComboBoxItem Content="18"></ComboBoxItem>
                <ComboBoxItem Content="19"></ComboBoxItem>
                <ComboBoxItem Content="20"></ComboBoxItem>
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction Command="{Binding PercentageChangedCommand}"
                                       CommandParameter="{Binding Percentage, ElementName=comboBox1}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </ComboBox>

when am accessing the selected value to a method in the combobox it only shows

system.windows.Controls.comboboxitem:12

It doesn't set that value.. help me plEASE

Upvotes: 0

Views: 371

Answers (1)

DenisPostu
DenisPostu

Reputation: 599

  1. If you use the code snippet above, an item in the combobox is a ComboBoxItem and this is why you get it in the setter for Percentage
  2. First of all remove SelectedValuePath="Percentage"
  3. Try removing all the ComboBoxItem declarations, expose the following property from the ViewModel and bind to it ItemsSource={Binding Items}

    public List<int> Items { get { return Enumerable.Range(1, 100).ToList(); } }

  4. Win! :)

Upvotes: 1

Related Questions