【问题标题】:How can i set the SelectedIndex of a combobox inside a GridView inside a ListView如何在 ListView 内的 GridView 内设置组合框的 SelectedIndex
【发布时间】:2019-02-12 13:39:13
【问题描述】:

我在 GridView (ListView) 中有一个组合框。我想使用代码隐藏设置 ComboBox 的 SelectedIndex。我尝试通过 XAML 绑定 SelectedIndex,但在通过 LVCriteria.Items.Add(object) 添加对象后,我得到了 ArgumentOutOfRangeException。该值为 2,因此在索引内。

<ListView x:Name="LVCriteria" Width="500" Height="230" SelectionChanged="LVCriteria_SelectionChanged" SelectionMode="Single" ScrollViewer.VerticalScrollBarVisibility="Auto" >                           
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Operator">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox x:Name="CBOperator" SelectedIndex="{Binding Operator}"HorizontalContentAlignment="Center" Style="{StaticResource MaterialDesignComboBox}" Width="50"  Padding="0,0,0,5" SelectionChanged="CBOperator_SelectionChanged">
                            <ComboBoxItem Background="{StaticResource MaterialDesignPaper}" Content="&lt;"/>
                            <ComboBoxItem Background="{StaticResource MaterialDesignPaper}" Content="&gt;"/>
                            <ComboBoxItem Background="{StaticResource MaterialDesignPaper}" Content="="/>
                            <ComboBoxItem Background="{StaticResource MaterialDesignPaper}" Content="&lt;="/>
                            <ComboBoxItem Background="{StaticResource MaterialDesignPaper}" Content="&gt;="/>
                        </ComboBox>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

班级是

public class PK
{ 
    public string ID { get; set; }
    public string Criteria { get; set; }
    public float? Value { get; set; }
    public int Operator { get; set; }
    public int Comp_id { get; set; }
    public int Type { get; set; }
}

【问题讨论】:

  • 欢迎来到 SO。您收到哪条错误消息?
  • 谢谢。我得到一个 ArgumentOutOfRangeException,但我检查了我的绑定值,它是 2,所以它在 ComboBox 索引内。

标签: wpf listview gridview combobox binding


【解决方案1】:

我不知道为什么会这样,但解决方案是 for 循环中的延迟。 我使用 for 循环将我的对象添加到 ListView。

更新:这是一个虚拟化问题。解决方案已设置

VirtualizingStackPanel.IsVirtualizing="False"

在列表视图中。

【讨论】:

    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 2011-08-16
    • 2017-04-14
    • 1970-01-01
    相关资源
    最近更新 更多