失去焦点时,在 WPF DatagridTextboxColumn 中输入的文本和在 DatagridComboboxColumn 中选择的值不会显示

发布于 2024-12-16 14:38:09 字数 3468 浏览 0 评论 0原文

我的问题可能看起来重复,因为 StackOverflow 中也有类似的问题,例如

https://stackoverflow.com/questions/5733782/how-to-在 wpfs-datagrid 的 datagridcombobox 列中保留值

https://stackoverflow.com/questions/ 6101441/creating-wpf-datagrid-dynamically-with-dynamically-created-drop-down-box-using-d

但我找不到有关的答案我的要求。

我动态生成一个 WPF 数据网格,它根据某些条件从元数据文件(excel)中获取列。数据网格需要是通用的,因此我无法在创建过程中绑定数据网格本身。我动态绑定 DatagridCombobobox 列,并且也能够获取其中的值。

现在我的问题是,一旦我在 DatagridCombobobox 中选择一个值或在 DatagridTextboxColumn 中输入文本,然后移动到另一个单元格/行,该文本和选择就会消失。

如何使数据网格单元格/行能够动态记住我输入的文本或选择? 我在 DataGridComboBoxColumn 单元格不显示所选项目文本中检查了类似问题的答案? 但它适用于静态数据网格。由于我的数据网格创建是动态的,我不知道如何使用这个解决方案。请帮忙。 我被困在这件事上,这花了我很多时间:(

问候, 尼兰詹。

动态 DataGrid 创建:

    Dim setupGrid = "<my:DataGrid AutoGenerateColumns='False' Name='grdSetup' " & "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'" & "  xmlns:sdk = 'http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk'" & "  xmlns:x = 'http://schemas.microsoft.com/winfx/2006/xaml'" & "  xmlns:TAB = 'http://fabtab.codeplex.com'" & "   xmlns:my='http://schemas.microsoft.com/wpf/2008/toolkit' " & " >"
    setupGrid = setupGrid & "<my:DataGrid.Columns>"
    ...
    ...
    ...
    setupGrid = setupGrid & "</my:DataGrid.Columns>"

    setupGrid = setupGrid & "</my:DataGrid>"

    Dim sr = New MemoryStream(Encoding.ASCII.GetBytes(setupGrid))
    Dim pc = New ParserContext()
    pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation")
    pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml")
    Dim grdElement As UIElement = DirectCast(System.Windows.Markup.XamlReader.Load(sr, pc), UIElement)
    Dim grdSetup As DataGrid = DirectCast(grdElement, DataGrid)

我的 DataGridCombobox 代码类似:

    setupGrid = setupGrid & "<my:DataGridComboBoxColumn x:Name='" & dr.Item("ParameterName").ToString.Trim & "'  Header='" & dr.Item("ParameterName") & "'  SelectedValuePath='" & dr.Item("ValueColumn") & "' DisplayMemberPath='" & dr.Item("DisplayColumn") & "'  />"

此字符串值将添加到上面的 DataGrid 字符串并转换为 UIElement,如上面的 DataGrid 代码所示。我动态地将数据网格添加到 ItemsControl 中:

    Me.DynamicContent.Items.Add(grdElement)

其中 DynamicContent 是 ItemsControl。

在 UserControl_Loaded 事件中,我通过以下方式找到数据网格中的每个组合框:

    Dim dynamicGrid as DataGrid = DynamicContent.ItemContainerGenerator.ContainerFromIndex(0)
    Dim comboColumns = dynamicGrid.Columns.OfType(Of DataGridComboBoxColumn)()
    For index As Integer = 0 To comboColumns.Count - 1
     comboColumns(index).ItemsSource = DirectCast(dt, IListSource).GetList()
    Next

其中 dt 是一个数据表,其中包含要发送到组合框的数据。它以组合框的 SelectedValuePath 和 DisplayMember 路径作为列。

我无法预先设置 DatagridComboboxColumn 的 SelectedItemBinding 和 SelectedValueBinding,因为它是动态的并且因不同的 ComboColumns 而异。我不知道如何在上面的加载事件中设置这些。 请让我知道我做错了什么?或者可以做什么,以便即使在焦点丢失后我也能够保留这些价值观。 抱歉冗长的描述:(

My question might seem repeatetive as there are similar questions in StackOverflow like,

https://stackoverflow.com/questions/5733782/how-to-retain-value-in-datagridcomboboxcolumn-of-wpfs-datagrid

https://stackoverflow.com/questions/6101441/creating-wpf-datagrid-dynamically-with-dynamically-created-drop-down-box-using-d

but I cudn't find an answer pertaining to my requirements.

I generate a WPF datagrid dynamically which takes columns from a metadata file(excel) based on certain conditions. The datagrid needs to be generic and hence I cannot bind the Datagrid itself during creation. I bind the DatagridCombobobox columns dynamically and was able to get the values in them too.

Now my problem is, once I select a value in the DatagridCombobobox or enter text in the DatagridTextboxColumn and then move to another cell/ row, this text and selection disappears.

How do I enable the Datagrid cell/row to remember my entered text or selection dynamically?
I checked the answer of a similar question in DataGridComboBoxColumn cell not displaying selected item text? but it is for a static datagrid. Since my datagrid creation is dynamic, I donot know how to use this solution. Kindly help.
I am stuck up in this and its taking a lot of my time :(

Regards,
Niranjan.

Dynamic DataGrid creation:

    Dim setupGrid = "<my:DataGrid AutoGenerateColumns='False' Name='grdSetup' " & "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'" & "  xmlns:sdk = 'http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk'" & "  xmlns:x = 'http://schemas.microsoft.com/winfx/2006/xaml'" & "  xmlns:TAB = 'http://fabtab.codeplex.com'" & "   xmlns:my='http://schemas.microsoft.com/wpf/2008/toolkit' " & " >"
    setupGrid = setupGrid & "<my:DataGrid.Columns>"
    ...
    ...
    ...
    setupGrid = setupGrid & "</my:DataGrid.Columns>"

    setupGrid = setupGrid & "</my:DataGrid>"

    Dim sr = New MemoryStream(Encoding.ASCII.GetBytes(setupGrid))
    Dim pc = New ParserContext()
    pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation")
    pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml")
    Dim grdElement As UIElement = DirectCast(System.Windows.Markup.XamlReader.Load(sr, pc), UIElement)
    Dim grdSetup As DataGrid = DirectCast(grdElement, DataGrid)

My DataGridCombobox code is similar :

    setupGrid = setupGrid & "<my:DataGridComboBoxColumn x:Name='" & dr.Item("ParameterName").ToString.Trim & "'  Header='" & dr.Item("ParameterName") & "'  SelectedValuePath='" & dr.Item("ValueColumn") & "' DisplayMemberPath='" & dr.Item("DisplayColumn") & "'  />"

This string value will be added to the above DataGrid string and converted to UIElement as shown in the above DataGrid code. I add my Datagrid to an ItemsControl dynamically :

    Me.DynamicContent.Items.Add(grdElement)

where DynamicContent is ItemsControl.

In the UserControl_Loaded event, I find each ComboBox in my Datagrid thru :

    Dim dynamicGrid as DataGrid = DynamicContent.ItemContainerGenerator.ContainerFromIndex(0)
    Dim comboColumns = dynamicGrid.Columns.OfType(Of DataGridComboBoxColumn)()
    For index As Integer = 0 To comboColumns.Count - 1
     comboColumns(index).ItemsSource = DirectCast(dt, IListSource).GetList()
    Next

where dt is a Datatable which consists of data to be to my ComboBox. It has the SelectedValuePath and DisplayMember path of the combobox as the columns.

I cannot set the SelectedItemBinding and the SelectedValueBinding for the DatagridComboboxColumn before hand as it is dynamic and varies for different ComboColumns. I dont know how these can be set in the above loaded event.
Pls let me know what I am doing wrong? Or what can be done so that I can be able to retain the values even after the focus is lost.
Sorry for the lengthy description :(

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文