访问组合框值以查看模型

本文关键字:模型 组合 访问 | 更新日期: 2023-09-27 18:33:22

Helo,我正在使用 mvvm...我有一个组合框,这是 XAML 代码

 <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>

当我访问组合框中方法的选定值时,它仅显示

system.windows.Controls.comboboxitem:12

它没有设置该值。帮我安慰

访问组合框值以查看模型

  1. 如果您使用上面的代码片段,则组合框中的项是组合框项,这就是为什么您在百分比的设置器中获取它的原因
  2. 首先删除SelectedValuePath="Percentage"
  3. 尝试删除所有 ComboBoxItem 声明,从 ViewModel 中公开以下属性并绑定到该属性ItemsSource={Binding Items}

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

  4. 赢了! :)