यह सुनिश्चित नहीं है कि यह अभी भी प्रासंगिक है लेकिन मुझे सेलटेम्प्लेट चयनकर्ता के साथ व्यक्तिगत कोशिकाओं को स्टाइल करने का एक तरीका मिला है।
public class DataMatrixCellTemplateSelectorWrapper : DataTemplateSelector
{
private readonly DataTemplateSelector _ActualSelector;
private readonly string _ColumnName;
private Dictionary<string, object> _OriginalRow;
public DataMatrixCellTemplateSelectorWrapper(DataTemplateSelector actualSelector, string columnName)
{
_ActualSelector = actualSelector;
_ColumnName = columnName;
}
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
// The item is basically the Content of the ContentPresenter.
// In the DataMatrix binding case that is the dictionary containing the cell objects.
// In order to be able to select a template based on the actual cell object and also
// be able to bind to that object within the template we need to set the DataContext
// of the template to the actual cell object. However after the template is selected
// the ContentPresenter will set the DataContext of the template to the presenters
// content.
// So in order to achieve what we want, we remember the original DataContext and then
// change the ContentPresenter content to the actual cell object.
// Therefor we need to remember the orginal DataContext otherwise in subsequent calls
// we would get the first cell object.
// remember old data context
if (item is Dictionary<string, object>)
{
_OriginalRow = item as Dictionary<string, object>;
}
if (_OriginalRow == null)
return null;
// get the actual cell object
var obj = _OriginalRow[_ColumnName];
// select the template based on the cell object
var template = _ActualSelector.SelectTemplate(obj, container);
// find the presenter and change the content to the cell object so that it will become
// the data context of the template
var presenter = WpfUtils.GetFirstParentForChild<ContentPresenter>(container);
if (presenter != null)
{
presenter.Content = obj;
}
return template;
}
}
नोट:: यह एक सा hacky क्योंकि आप सेल के लिए उचित DataContext पाने के लिए ContentPresenter की सामग्री के साथ चारों ओर चबाना करने के लिए है (ताकि आप सेल टेम्पलेट में वास्तविक सेल आइटम करने के लिए बाध्य कर सकते हैं) है मैं DataMatrix को CodeProject आलेख से बदल दिया ताकि पंक्तियां शब्दकोश (कॉलमनाम -> सेल ऑब्जेक्ट) हों।
मैं गारंटी नहीं दे सकता कि यह समाधान कुछ तोड़ नहीं देगा या भविष्य में तोड़ नहीं देगा।नेट रिलीज यह इस तथ्य पर निर्भर करता है कि सामग्री प्रदाता टेम्पलेट को अपनी सामग्री में चुनने के बाद DataContext सेट करता है। (परावर्तक इन मामलों में एक बहुत मदद करता है :))
जब GridColumns बनाने, मैं ऐसा ही कुछ:
var column = new GridViewColumn
{
Header = col.Name,
HeaderTemplate = gridView.ColumnHeaderTemplate
};
if (listView.CellTemplateSelector != null)
{
column.CellTemplateSelector = new DataMatrixCellTemplateSelectorWrapper(listView.CellTemplateSelector, col.Name);
}
else
{
column.DisplayMemberBinding = new Binding(string.Format("[{0}]", col.Name));
}
gridView.Columns.Add(column);
नोट: मैं ListView विस्तार किया है इतना है कि यह एक CellTemplateSelector संपत्ति आप करने के लिए बाध्य कर सकते हैं XAML में
@Edit 15/03/2011: http://codesilence.wordpress.com/2011/03/15/listview-with-dynamic-columns/
इस तरह मैं करना चाहता हूं लेकिन यह स्पष्ट नहीं करता कि कैसे। संलग्न प्रॉप्स और डीपी अभी भी मेरे लिए एक रहस्य हैं। किसी भी उदाहरण की सराहना की जाएगी। इस पर एक नज़र डालें (https://rachel53461.wordpress.com/2011/09/17/wpf-grids-rowcolumn-count-properties/)। क्या यह है कि यह कैसे है? – VivekDev