以编程方式设置DataGrid.SelectedItem [英] ProgrammaticallySet DataGrid.SelectedItem

查看:85
本文介绍了以编程方式设置DataGrid.SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个 DataGrids ,它们存储一个唯一的ID,然后存储有关该人,公司等的多列信息. DataGrid TabControl 一起存储,并带有单独的 TabItem ,用于填写有关人员/公司的详细信息.当用户单击添加时,我想将它们带回到第一个 TabItem ,并自动选择用户添加的新项目(个人或公司).

I have multiple DataGrids that store a unique ID, then multiple columns of information regarding that person, company etc. The DataGrid is stored with a TabControl, with a separate TabItem that is used to fill in details regarding the person/company. When the user clicks add, I would like to take them back to the first TabItem, and automatically select the new Item (person or company) that the user has added.

我已经可以使用 TabItem 来设置;

I can already set the TabItem back using;

tabItem.IsSelected = true

有没有一种方法,单击添加后,我设置 TabItem ,循环浏览 DataGrid ,找到新项目的ID并设置 SelectedItem 要吗?当我将其分配给人员/公司时,我会知道该项目的ID.

Is there a way in which, after clicking add, I set the TabItem, loop through the items in the DataGrid, find the ID of the new item and set the SelectedItem to it? I would know the ID of the Item as I assign it to the person/company.

推荐答案

您是否已经问过这个问题?

Didn't you already ask this question?

为父窗口定义视图模型.

Define a viewmodel for the parent window.

为Observablecollection< Person>添加属性.并将您的数据网格绑定到使用该observablecollection作为源的collectionview源.

Add properties for that for an Observablecollection<Person> and bind your datagrid to a collectionviewsource which uses that observablecollection as the source.

添加Person对象时,请将其添加到基础的observablecollection中,并使其成为collectionviewsource中的当前项.

When you add your Person object, add it to that underlying observablecollection and make it the current item in the collectionviewsource.

在数据网格中使用与此类似的标记:

Use markup similar to this in your datagrid:

        <DataGrid x:Name="dg" AutoGenerateColumns="False"
                  ItemsSource="{Binding PeopleCollectionView}"
                  IsSynchronizedWithCurrentItem="True"
                  EnableRowVirtualization="True"
                  HeadersVisibility="Column"
                  >
            <i:Interaction.Behaviors>
                <support:ScrollDataGridRowIntoView />
            </i:Interaction.Behaviors>
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="support:DataGridRowBehavior.IsDataGridRowFocussedWhenSelected" Value="true"/>
                </Style>
            </DataGrid.RowStyle>

并为此添加示例中的两种行为:

And add the two behaviours from the sample for this:

http://social.technet.microsoft.com/wiki/contents/articles/29859.wpf-tips-bind-to-current-item-of-collection.aspx

当您使该项目成为当前项目时,请确保您的数据网格仍在可视树中,并且它将通过将记录置于视图"目录中来进行响应.并选择它.

Make sure your datagrid is still there in the visual tree when you make that item current and it will respond by bringing the record into "view" and selecting it.

那个样本甚至有一群人.

That sample even has a collection of people.

请注意,如果您在人员视图中更改当前项目,则该项目将成为在人员视图中显示的人员.同样,如果您击中任何一个三角形以亲自进行导航,则人员数据网格中的选定项目会做出响应.

Notice that if you change the current item in the people view it becomes the person shown in person view. Similarly, if you hit either triangle to navigate in person, the selected item in the people datagrid responds.

PS
您可以使用movecurrentto在collectionview中使当前内容变为当前状态:

PS
You make something current in a collectionview using movecurrentto:

PeopleView.MoveCurrentTo(NewPerson);



这篇关于以编程方式设置DataGrid.SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆