Hi there,
i have a small project with a DataGrid where one field is a CheckBoxColumn.
i set the CheckBox in the Column with the following Style in code behind:
UnboundField checkfield = new UnboundField(); ControlTemplate controltemplate; string xamlControl = "";
xamlControl = @" <ControlTemplate xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""> <CheckBox HorizontalAlignment=""Center"" IsChecked=""{Binding Attributes[Used]}"" VerticalAlignment=""Center""/> </ControlTemplate>";
byte[] xmlControl = Encoding.ASCII.GetBytes(xamlControl); MemoryStream stream = new MemoryStream(xmlControl); controltemplate = (ControlTemplate)XamlReader.Load(stream); Setter templateSetter = new Setter(CellValuePresenter.TemplateProperty, controltemplate); Style checkboxcell = new Style(typeof(CellValuePresenter)); checkboxcell.Setters.Add(templateSetter); checkfield.Settings.CellValuePresenterStyle = checkboxcell;
Everything is working fine and now i come to my problem.
i need to read every record in the grid and extract the boolean IsChecked-Value of the CheckBoxColumn for each record.
How do i handle this?
regards
Cloud
Cloud Strife said: Hi there, i have a small project with a DataGrid where one field is a CheckBoxColumn. i set the CheckBox in the Column with the following Style in code behind: UnboundField checkfield = new UnboundField(); ControlTemplate controltemplate; string xamlControl = ""; xamlControl = @" <ControlTemplate xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""> <CheckBox HorizontalAlignment=""Center"" IsChecked=""{Binding Attributes[Used]}"" VerticalAlignment=""Center""/> </ControlTemplate>"; byte[] xmlControl = Encoding.ASCII.GetBytes(xamlControl); MemoryStream stream = new MemoryStream(xmlControl); controltemplate = (ControlTemplate)XamlReader.Load(stream); Setter templateSetter = new Setter(CellValuePresenter.TemplateProperty, controltemplate); Style checkboxcell = new Style(typeof(CellValuePresenter)); checkboxcell.Setters.Add(templateSetter); checkfield.Settings.CellValuePresenterStyle = checkboxcell; regards Cloud
i tried another way:
UnboundField checkfield = new UnboundField(); checkfield.Name = ""; checkfield.Settings.EditorType = typeof(XamCheckEditor); checkfield.Settings.EditAsType = typeof(bool); Style editorStyle = new Style(typeof(XamCheckEditor)); Setter styleSetter = new Setter(XamCheckEditor.IsCheckedProperty, dictionary["Used"]); editorStyle.Setters.Add(styleSetter); checkfield.Settings.EditorStyle = editorStyle; checkfield.Settings.AllowEdit = true; checkfield.Width = new FieldLength(50); grid.FieldLayouts[0].Fields.Add(checkfield);
this works as good as the other way. But now i have the problem that if i want to get the Value of the cell where my checkbox is in, the value is null.
i noticed that i have to edit one time so that the value of the cell is filled with true or false. but i need that the value is loaded at the first loading of the window.
please help me asap
cloud
Hello Cloud,
I am just checking the progress of this issue and was wondering if you managed to achieve your goal or if you need any further assistance on the matter.
Thank you for your post. I have been looking into it and I can suggest you use the following code in order to get the ActiveRecord’s CheckBox value:
CellValuePresenter cvp = CellValuePresenter.FromRecordAndField(xamDataGrid1.ActiveRecord as DataRecord, xamDataGrid1.FieldLayouts[0].Fields["name or index of the UnboundField"]); CheckBox cb = Utilities.GetDescendantFromType(cvp, typeof(CheckBox), true) as CheckBox;
Please let me know if this helps you or you need further assistance on this matter.
Looking forward for your reply.