0
我正在嘗試使用標籤和列標題的組合框創建自定義列。 網格首次正確加載。點擊按鈕後,我更改Datagrid的資源。我看到數據網格的單元格得到更新,但不是標題。我也看到Datagrid_Loaded沒有被調用。在調試我發現,對於「綁定」對象我公司供應的綁定路徑,但頭我公司供應的源,因此頭沒有更新。如何爲頭部提供路徑,使其與的綁定相似綁定?WPF自定義列未正常工作的Datagrid標頭綁定
private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
DataGrid grid = sender as DataGrid;
if (grid.Items.Count == 0)
{
return;
}
StageVM data = grid.Items[0] as StageVM;
if (data == null)
{
return;
}
int index = 0;
foreach (StageItemVM param in data.Stage)
{
var binding = new Binding(string.Format("Stage[{0}].StageItem.Value", index));
grid.Columns.Add(new CustomBoundColumn()
{
CanUserSort = false,
HeaderTemplateSelector = this.FindResource("ColumnHeaderTemplateSelector") as DataTemplateSelector,
HeaderStyle = this.FindResource("StageHeaderStyle") as Style,
Header = (new Binding().Source = param.StageItem.Key),
Binding = binding,
TemplateSelectorName = "StageRangeColumnTemplateSelector"
});
index++;
}
}
而且customBoundColumn類如下:
public class CustomBoundColumn : DataGridBoundColumn
{
public string TemplateSelectorName { get; set; }
protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
var binding = new Binding(((Binding)Binding).Path.Path);
binding.Source = dataItem;
var content = new ContentControl();
content.ContentTemplateSelector =(DataTemplateSelector)cell.FindResource(TemplateSelectorName);
content.SetBinding(ContentControl.ContentProperty, binding);
return content;
}
protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
{
return GenerateElement(cell, dataItem);
}
}
第一次綁定適合我。但只要我更改itemssource就會創建問題。 [鏈接]:(http://stackoverflow.com/questions/21853326/c-sharp-header-in-the-grid-not-getting-refreshed-updated)這說明確切的問題 –