My question might seem repeatetive as there are similar questions in StackOverflow like,
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 :(