存储DataGrid SelectedItem WPF C# [英] Storing a DataGrid SelectedItem WPF C#

查看:97
本文介绍了存储DataGrid SelectedItem WPF C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TabControl ,其中包含三项,一项用于查看公司列表,一项用于更新公司,另一项用于添加新公司.当用户在 DataGrid 中选择公司时, 填写了与公司详细信息绑定的更新 TabItem 中的 TextBoxes 设置了 SelectedItem 属性.这些 TextBoxes 的绑定方式是这样;

I have a TabControl that has three items, one to view a list of companies, one to update a company and one to add a new company. When the user selects a company in the DataGrid, a number of TextBoxes in the update TabItem that are bound to the Company's details are filled in, and the DataGrid's SelectedItem property is set. These TextBoxes are bound like so;

    <TextBox Text="{Binding SelectedItem.CompanyName, ElementName=dataGrid}" VerticalScrollBarVisibility="Visible" TextWrapping="Wrap" Margin="10" IsReadOnly="true" MaxHeight="27"/>
    <TextBox Text="{Binding SelectedItem.CompanyAddr1, ElementName=dataGrid}" Margin="10,0,10,10" Height="26" IsReadOnly="true"/>
    <TextBox Text="{Binding SelectedItem.CompanyAddr2, ElementName=dataGrid}" Margin="10,0,10,10" IsReadOnly="true"/>
    <TextBox Text="{Binding SelectedItem.CompanyTown, ElementName=dataGrid}" Margin="10,0,10,10" IsReadOnly="true"/>
    <TextBox Text="{Binding SelectedItem.CompanyCounty, ElementName=dataGrid}" Margin="10,0,10,10" IsReadOnly="true"/>
    <TextBox Text="{Binding SelectedItem.CompanyPcode, ElementName=dataGrid}" Margin="10,2,10,10" IsReadOnly="true"/>


一切正常,但是当我单击按钮更新公司时出现问题,单击按钮的代码可以在此处查看;
   


This all works fine, however the issue comes when I click on the button to update a company, the code for the button click can be seen here;
   

private void UpdateCompany(object sender, RoutedEventArgs e)
        {
            var cm_UpdateService = new CompanyUpdateService();
            var companyDetails = new CompanyModel();
            var selectedCompany = dataGrid.SelectedItem as CompanyModel;

            companyDetails.CompanyID = selectedCompany.CompanyID;
            companyDetails.CompanyName = nameTextBox.Text.Trim();
            companyDetails.CompanyAddr1 = addr1TextBox.Text.Trim();
            companyDetails.CompanyAddr2 = addr2TextBox.Text.Trim();
            companyDetails.CompanyTown = townTextBox.Text.Trim();
            companyDetails.CompanyCounty = countyTextBox.Text.Trim();
            companyDetails.CompanyPcode = pCodeTextBox.Text.Trim();
            companyDetails.CompanyTelephone = telTextBox.Text.Trim();
            companyDetails.CompanyAltTelephone = altTelTextBox.Text.Trim();
            companyDetails.CompanyFax = faxTextBox.Text.Trim();
            companyDetails.CompanyEmail = emailTextBox.Text.Trim();
            companyDetails.CompanyGeographicArea = geoAreaTextBox.Text.Trim();
            companyDetails.CompanySearchName = searchNameTextBox.Text.Trim();

            try
            {
                if (CheckCompanyUpdateIntegrity(companyDetails.CompanyID))
                {
                    if (cm_UpdateService.HandleCompanyUpdate(companyDetails)) // Update Company Details 
                    {
                        FillDataGrid();
                        tabControl.SelectedIndex = 0;
                        MessageBox.Show("Company details updated.");
                    }
                    else
                    {
                        MessageBox.Show("Could not update Company Details.");
                    }
                }
                else
                {
                    MessageBox.Show("Sorry Someone has Updated this company");
                }
            }
            catch (Exception ex)
            {
                var hEs = new HandleExceptionService();
                hEs.HandleException(ex.ToString());
            }
        }



用户单击更新后,它们将被带回到包含以下内容的第一个 TabItem : 所有公司的 DataGrid ( tabControl.SelectedIndex = 0; ).我想做的就是存储 SelectedItem ,以便将它们立即带回到他们更新的公司,但是相反,他们需要再次选择同一公司以查看其更新的详细信息.有没有办法我可以存储 在 TabItems 之间的 SelectedItem ,这样,在更新公司后,用户将被带回到第一个 Tabitem ,并且已经选择了他们更新的公司,并且



After the user clicks update, they are taken back to the first TabItem containing the DataGrid of all companies (tabControl.SelectedIndex = 0;). What I would like to do is store the SelectedItem so that they are immediately taken back to the Company they updated, but instead they need to select the same Company again to see the details they have updated. Is there a way that I can store the SelectedItem between TabItems so that after updating a company the user is taken back to the first Tabitem, and the company they have updated is already selected and the TextBoxes filled in?

推荐答案

我会将公司集合放在窗口的数据上下文中.

I would put the companies collection in the datacontext of the window.

然后您可以从标签页中绑定到该页面.

You can then bind to that from your tabs.

如果您使用此处说明的当前项目机制:

If you use the current item mechanism explained here:

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

如果尝试该示例,您将注意到您可以在个人的单个记录视图中导航,并且该视图反映在datagrid中.这些是完全不同的用户控件,它们的链接纯粹是它们从数据上下文绑定到的集合.

If you try that sample you will notice you can navigate in the single record view for person and it is reflected in the datagrid. These are in entirely different usercontrols whose link is purely the collection they bind to from their datacontext.

但是,基本上,您可以将这两个用户控件放在该窗口的单独选项卡中,并且仍然可以使用.

But, basically, you could put those two usercontrols in separate tabs in that window and it'd still work.

如果您随后添加了另一个标签,则该标签添加了一个项目并使用了movecurrent到(theitemadded),并且该选项卡将成为人员视图中的选定和重点突出的项目.

If you then added another tab that added an item and used movecurrentto(theitemadded) and that would become the selected and focussed item in the people view. 

您可以使添加的新行成为当前行,并使用示例中的两种行为.

You can make your new row you added current and use the two behaviours which are in the sample.

在支持"文件夹中,您将看到DataGridRowBehavior和ScrollDataGridRowIntoView.

In the Support folder you'll see DataGridRowBehavior and ScrollDataGridRowIntoView.

在人物"视图中使用了哪些(人物"数据网格用户控件)

Which are used in the People view ( the People datagrid usercontrol )

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

请注意,将焦点设置在数据网格中的一行上并非易事.它们是虚拟化的,如果您尝试使用数据网格本身中的行,则会发现一些不可见"的内容.甚至没有一个可以滚动到的数据网格.

Note that setting focus on a row in a datagrid is non trivial. They are virtualised and if you try and use the row in the datagrid itself you'll find something that isn't "visible" doesn't even have a datagridrow that you could scroll to.

使用这些行为,collectionview并与currentitem同步可以避免该问题.我在上面加粗了三个重要的标记.

Using those behaviours, a collectionview and issynchronisedwithcurrentitem avoids that problem. I've bolded the three critical pieces of markup above.

.

您可以在本文中了解有关collectionview的更多信息:

You can read more about collectionview in this article:

http://social.technet.microsoft.com/wiki /contents/articles/26673.wpf-collectionview-tips.aspx

.

很清楚吗?


这篇关于存储DataGrid SelectedItem WPF C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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