有谁知道我可以如何在代码中执行等效的XAML绑定?代码中的DataGridColumn绑定
<DataGrid ... >
<DataGrid.Columns>
<DataGridTextColumn
Binding="{Binding Description}" <=== set in code **
/>
</DataGrid.Columns>
</DataGrid>
干杯,
Berryl
=== UPDATE ====
它看起来像我一直在寻找的方法是DataGridColumn.GenerateElement
如果是这样,那么这个问题的焦点是如何正确设置Binding。我想做这个代码的原因是我的网格有7列在视觉上是完全相同的,并且其数据可以被索引所知。
所以我希望能够通过使用具有索引属性的子类DataGridTextColumn简化XAML,只是有:
<DataGrid ... >
<DataGrid.Columns >
<local:DayOfWeekColumn Index="0" />
<local:DayOfWeekColumn Index="1" />
....
<local:DayOfWeekColumn Index="7" />
</DataGrid.Columns >
</DataGrid >
===经修订的课题===
假设绑定本身在逻辑上和语法上是正确的,参数BindingOperations.SetBinding应该是什么?
protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) {
var activity = (ActivityViewModel)dataItem;
var cellData = activity.Allocations[Index];
var b = new Binding
{
Source = cellData,
UpdateSourceTrigger = UpdateSourceTrigger.LostFocus,
Converter = new AllocationAmountConverter()
};
BindingOperations.SetBinding(??, ??, b);
return ??;
}
=== EDITS为ARAN =====
我不重写GenerateElement现在,而是试图让一个静态辅助设置我为我结合。在任何情况下都需要帮助程序来补偿无法绑定当前MSFT DataGrid实现中的标题内容。
基本上这个想法是从网格中捕获DC,并根据需要在每个列上使用DC,在这种情况下,这些列将是Header内容,单元格样式和Binding。下面是代码:
public class TimesheetDataGridColumnContextHelper
{
static TimesheetDataGridColumnContextHelper() {
FrameworkElement.DataContextProperty.AddOwner(typeof (DataGridTextColumn));
FrameworkElement.DataContextProperty.OverrideMetadata(
typeof (DataGrid),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, OnDataContextChanged));
}
public static void OnDataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var grid = d as DataGrid;
if (grid == null || !grid.Name.Equals("adminActivityGrid")) return;
foreach (var col in grid.Columns) {
var dowCol = col as DayOfTheWeekColumn;
if (dowCol == null) continue;
var context = (IActivityCollectionViewModelBase) e.NewValue;
var index = Convert.ToInt32(dowCol.DowIndex);
_setHeader(dowCol, context, index);
var editStyle = (Style) grid.FindResource("GridCellDataEntryStyle");
dowCol.CellStyle = editStyle;
_setBinding(dowCol, index, context);
}
}
private static void _setBinding(DayOfTheWeekColumn dowCol, int index, IActivityCollectionViewModelBase context) {
dowCol.Binding = new Binding
{
Path = new PropertyPath(string.Format("Allocations[{0}]", index)),
UpdateSourceTrigger = UpdateSourceTrigger.LostFocus,
Converter = new AllocationAmountConverter()
};
}
private static void _setHeader(DataGridColumn col, IActivityCollectionViewModelBase context, int index)
{
var date = context.HeaderDates[index];
var tb = new TextBlock
{
Text = date.ToString(Strings.ShortDayOfWeekFormat),
ToolTip = date.ToLongDateString()
};
col.Header = tb;
}
}
}
一切正常,除了绑定。我不知道是不是因为我的绑定是错误的(尽管我没有明显的错误),或者这不是一个设置它的好地方。当我运行它时,网格列只是空的。
任何想法??
干杯,
Berryl
===修复! ===
上次更新中的逻辑实际上是正确的,但在DataGrid的内部丢失了我错过了我的Binding.Path缺少要绑定的属性!感谢Aran了解这个问题,意识到GenerateElement覆盖不是必需的,并且捕捉到绑定源不应该被设置。
嗨阿兰。是的,我的小提琴正在为此做一个锻炼,但似乎值得一试。请参阅我上次的编辑(“编辑ARAN”)以获取最新方法。干杯! – Berryl 2010-08-09 21:35:31
明白了。是我的绑定路径中的一个蹩脚的语法错误(应该是“Allocations [{0}]。Amount”)感谢您的帮助! – Berryl 2010-08-11 00:18:40