So I know that I can put multiple fieldlayout nodes in my XAML and then, programmatically, change which one is used in the AssigningFieldLayoutToItem event. But what I'd like to do is something a little more databinding-y.
Is there any way that I could create a property on my underlying object (serving as the DataContext of the window) that is either a FieldLayout object or a collection of Field objects and use databinding to hook up the grid's fieldlayout to that property?
That would certainly be a far more flexible approach. Let me know if anyone out there has any ideas.
HI,
The FieldLayout is not an actual visual element in the visual tree. This fact will prevent you from binding to it.
Sincerely, MattDeveloper Support Engineer
Hello,
You could use an attached property that you can bind to the XamDataGrid, which will handle the rest automatically. Here is what I am suggesting :
public static ObservableCollection <FieldLayout > GetAttachedFieldLayouts(DependencyObject obj)
{
return (ObservableCollection <FieldLayout >)obj.GetValue(AttachedFieldLayoutsProperty);
}
public static void SetAttachedFieldLayouts(DependencyObject obj, ObservableCollection <FieldLayout > value)
obj.SetValue(AttachedFieldLayoutsProperty, value);
public static readonly DependencyProperty AttachedFieldLayoutsProperty =
DependencyProperty .RegisterAttached("AttachedFieldLayouts" ,
typeof (ObservableCollection <FieldLayout >),
typeof (FieldLayoutAdvisor ),
new UIPropertyMetadata (null , new PropertyChangedCallback (OnAttachedFieldLayoutsChanged)));
private static void OnAttachedFieldLayoutsChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
DataPresenterBase dpb = o as DataPresenterBase ;
if (dpb != null )
if (dpb.FieldLayoutSettings == null )
dpb.FieldLayoutSettings = new FieldLayoutSettings ();
dpb.FieldLayoutSettings.AutoGenerateFields = false ;
ObservableCollection <FieldLayout > collection = args.NewValue as ObservableCollection <FieldLayout >;
if (collection != null && collection.Count > 0)
dpb.FieldLayouts.Clear();
foreach (FieldLayout fl in collection)
dpb.FieldLayouts.Add(fl);
Then, you can bind the XamDataGrid to your DataContext :
<igDP:XamDataGrid
Name="xamDataGrid1"
DataSource="{Binding Path=Source}"
fla:FieldLayoutAdvisor.AttachedFieldLayouts="{Binding FieldLayouts}"/>
HI,.
I am just following up on this forum thread.
Do you have further questions regarding this issue.
Sincerley,
Matt Developer Support Engineer
Matt,
Thanks, but not at this time. I'll try Alex's suggestion if I can ever find my way back to working on the problem that prompted my question.
The joys of frantic development!
I am using the XamDataGrid and have a requirement where I need to generate the columns and rows dynamically at the initialization. For this what I have done is, I have an ObservableCollection of MyGridRow in my View Model which in turn has an ObservableCollection of string. In this way, I am generating each cell for a row and multiple rows respectively. The ObservableCollection<MyGridRow> serves as the data source to the XamDataGrid.
I implemented the FieldLayoutAdvisor as per Alex's solution and populated the field layouts. But nothing is showing up on the grid.
The code this way -
FieldLayout headerFieldLayout = new FieldLayout();
XDocument listDoc = XDocument.Load("SecMaster.XML", LoadOptions.None);
//Get the headers of the list to create the fields in the xamDataGrid
if (listDoc.Root != null) {
var fieldHeaders = listDoc.Root.Element("SECMASTERREC").Descendants().Select(node => node.Name.LocalName);
if (fieldHeaders.Count() > 0)
var fieldsList = fieldHeaders.ToList();
for (int index = 0; index < fieldsList.Count; index++)
Field headerField = new Field();
headerField.Name = fieldsList[index];
headerField.Label = fieldsList[index];
headerFieldLayout.Fields.Add(headerField);
if (headerFieldLayout.Fields != null && headerFieldLayout.Fields.Count > 0)
if (ListsFieldLayouts == null) ListsFieldLayouts = new ObservableCollection<FieldLayout>();
ListsFieldLayouts.Add(headerFieldLayout);
var gridCellRow = listDoc.Descendants("SECMASTERREC");
if (gridCellRow.Count() > 0)
var gridCellRowList = gridCellRow.ToList();
ListGridRow gridRow = new ListGridRow();
for (int index = 0; index < gridCellRowList.Count; index++)
gridRow.ListGridCells.Add(new ListGridCell { GridCellValue = gridCellRowList[index].Value });
if (ListsGridSource == null) ListsGridSource = new ObservableCollection<ListGridRow>();
ListsGridSource.Add(gridRow);