如何在 ListView 内的 GridView 内设置组合框的 SelectedIndex
How can i set the SelectedIndex of a combobox inside a GridView inside a ListView
我在 GridView (ListView) 中有一个组合框。我想使用代码隐藏设置 ComboBox 的 SelectedIndex。我尝试通过 XAML 绑定 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="<"/>
<ComboBoxItem Background="{StaticResource MaterialDesignPaper}" Content=">"/>
<ComboBoxItem Background="{StaticResource MaterialDesignPaper}" Content="="/>
<ComboBoxItem Background="{StaticResource MaterialDesignPaper}" Content="<="/>
<ComboBoxItem Background="{StaticResource MaterialDesignPaper}" Content=">="/>
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Class 是
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; }
}
我不知道为什么会这样,但解决方案是在 for 循环中延迟。
我使用 for 循环将我的对象添加到 ListView 中。
更新:这是一个虚拟化问题。解决方案设置
VirtualizingStackPanel.IsVirtualizing="False"
在列表视图中。
我在 GridView (ListView) 中有一个组合框。我想使用代码隐藏设置 ComboBox 的 SelectedIndex。我尝试通过 XAML 绑定 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="<"/>
<ComboBoxItem Background="{StaticResource MaterialDesignPaper}" Content=">"/>
<ComboBoxItem Background="{StaticResource MaterialDesignPaper}" Content="="/>
<ComboBoxItem Background="{StaticResource MaterialDesignPaper}" Content="<="/>
<ComboBoxItem Background="{StaticResource MaterialDesignPaper}" Content=">="/>
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Class 是
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; }
}
我不知道为什么会这样,但解决方案是在 for 循环中延迟。 我使用 for 循环将我的对象添加到 ListView 中。
更新:这是一个虚拟化问题。解决方案设置
VirtualizingStackPanel.IsVirtualizing="False"
在列表视图中。