我如何在ListView内的GridView内设置组合框的SelectedIndex

时间:2018-09-07 08:29:55

标签: wpf listview gridview combobox binding

我在GridView(ListView)内有一个组合框。我想将Code-behind设置为ComboBox的SelectedIndex。我通过XAML与Binding SelectedIndex进行了尝试,但是在通过LVCriteria.Items.Add(object)添加对象后得到了ArgumentOutOfRangeException。该值为2,因此在Index内。

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

1 个答案:

答案 0 :(得分:0)

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

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

VirtualizingStackPanel.IsVirtualizing =“ False”

在ListView中。