选择新行后,ListView 行保持选中状态

发布于 2024-12-15 10:46:27 字数 3779 浏览 1 评论 0原文

以下 xaml 代码生成一个包含三列的 ListView。 ListView ItemsSource 是一个可观察的集合。第一列显示特定行中对象的名称。第二列和第三列显示与特定行中的对象关联的按钮。

<Grid Width="497" Height="260">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="309*" />
        <ColumnDefinition Width="188*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="478*" />
        <RowDefinition Height="4*" />
    </Grid.RowDefinitions>
    <GroupBox Header="ObservableCollection Object List" HorizontalAlignment="Left" Width="497" BorderBrush="Black" BorderThickness="2" Grid.ColumnSpan="2">
        <ListView ItemsSource="{Binding Path=CollectionList}" Name="QueueListView">
            <ListView.Resources>
                <Style TargetType="{x:Type ListViewItem}">
                    <EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
                </Style>
            </ListView.Resources>
            <ListView.SelectedItem>
                <Binding Path="SelectedObjectFromList" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" >
                </Binding>               
            </ListView.SelectedItem>
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="140" Header="Object Name" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Width="160" Header="Property Information">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Get Property Info" Command="{Binding Path=GetObjProperties}"
                                        DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" />                      
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="180" Header="Transfer">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Transfer Object" Command="{Binding Path=TransferObjHere}"
                                        DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </GroupBox>
</Grid>

当用户单击“传输对象”或“获取属性信息”按钮时,SelectCurrentItem 事件处理程序将 ListView.SelectedItem 绑定到我的视图模型中的 SelectedObjectFromList 属性。我使用此属性将所选对象公开给我的视图模型。

这是我的 SelectCurrentItem 处理程序 c# 代码,位于代码后面:

    protected void SelectCurrentItem(Object sender, KeyboardFocusChangedEventArgs e)
    {
        ListViewItem viewItem = (ListViewItem)sender;
        viewItem.IsSelected = true;
    }

这非常有用!当用户单击按钮时,将从该行的 ListView observablecollection 对象中更新 SelectedObjectFromList 属性。 (在单击按钮之前无需手动单击 ListView 行来设置属性。)

一个问题:当我单击列表中的按钮时,最近选择的行仍然显示为被选中(它们在 GUI 中突出显示)。

我尝试通过设置 ListView 的 isFocused 属性来解决问题:

    protected void SelectCurrentItem(Object sender, KeyboardFocusChangedEventArgs e)
    {
        ListViewItem Item = (ListViewItem)sender;
        Item.IsSelected = true;
        Item.IsFocused = true;
    }

当然,这给了我一个 StackOverflow 例外:)。在这种情况下,有人有示例代码来更新 GUI 中的 ListView.Selection 吗?提前致谢。

The following xaml code produces a ListView with three columns. The ListView ItemsSource is an observablecollection. The first column shows the name of the object in a particular row. The second and third columns show buttons associated to the object in a particular row.

<Grid Width="497" Height="260">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="309*" />
        <ColumnDefinition Width="188*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="478*" />
        <RowDefinition Height="4*" />
    </Grid.RowDefinitions>
    <GroupBox Header="ObservableCollection Object List" HorizontalAlignment="Left" Width="497" BorderBrush="Black" BorderThickness="2" Grid.ColumnSpan="2">
        <ListView ItemsSource="{Binding Path=CollectionList}" Name="QueueListView">
            <ListView.Resources>
                <Style TargetType="{x:Type ListViewItem}">
                    <EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
                </Style>
            </ListView.Resources>
            <ListView.SelectedItem>
                <Binding Path="SelectedObjectFromList" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" >
                </Binding>               
            </ListView.SelectedItem>
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="140" Header="Object Name" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Width="160" Header="Property Information">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Get Property Info" Command="{Binding Path=GetObjProperties}"
                                        DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" />                      
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="180" Header="Transfer">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Transfer Object" Command="{Binding Path=TransferObjHere}"
                                        DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </GroupBox>
</Grid>

The SelectCurrentItem event handler binds the ListView.SelectedItem to the SelectedObjectFromList property in my view model when the user clicks the "Transfer Object" or "Get Property Info"button. I an use this property to expose the selected object to my view model.

Here is my SelectCurrentItem handler c# code in code behind:

    protected void SelectCurrentItem(Object sender, KeyboardFocusChangedEventArgs e)
    {
        ListViewItem viewItem = (ListViewItem)sender;
        viewItem.IsSelected = true;
    }

This works great! When the user has clicked a button, the SelectedObjectFromList property is updated from the ListView observablecollection object for that row. (No need to manually click the ListView row to set the property before clicking the button.)

One problem: As I click buttons in the list, the recently selected rows still appear to be selected (they are highlighted in the GUI).

I tried to solve the problem by setting the isFocused property of the ListView:

    protected void SelectCurrentItem(Object sender, KeyboardFocusChangedEventArgs e)
    {
        ListViewItem Item = (ListViewItem)sender;
        Item.IsSelected = true;
        Item.IsFocused = true;
    }

Of course that gives me a StackOverflow exeption :). Does anyone have example code to update the ListView.Selection in the GUI in this case? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟到的我 2024-12-22 10:46:27

ListView 允许您一次选择多个项目。当您单击第二个项目时,您不会将旧选定项目的 IsSelected 属性重置为 False,因此它保持选中状态

您可以尝试设置 ListView 的 SelectionMode 到 Single

<ListView SelectionMode="Single" ... />

或尝试获取旧的 SelectedItem 并在选择新的时取消选择它

protected void SelectCurrentItem(Object sender, KeyboardFocusChangedEventArgs e)
{
    var temp = myListBox.SelectedItem as ListViewItem;
    if (temp != null)
        temp.IsSelected = false;

    ListViewItem viewItem = (ListViewItem)sender;
    viewItem.IsSelected = true;
}

ListViews allow you to have multiple items selected at once. When you click on a 2nd item, you're not resetting the IsSelected property of the old selected item to False, so it is staying selected

You can try either setting the ListView's SelectionMode to Single

<ListView SelectionMode="Single" ... />

or try getting the old SelectedItem and unselecting it when you're selecting the new one

protected void SelectCurrentItem(Object sender, KeyboardFocusChangedEventArgs e)
{
    var temp = myListBox.SelectedItem as ListViewItem;
    if (temp != null)
        temp.IsSelected = false;

    ListViewItem viewItem = (ListViewItem)sender;
    viewItem.IsSelected = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文