听起来很像关于绑定标题的其他问题 - 但通常它们是关于简单的标题,如TextBoxes ....
我的标题需要使用一个对象实例化,其中包含多个图像和文本组件,称为EMSPlanViewDeviceNew。它有一个名为TheDeviceProperty的DP,它包含设备的所有数据。
这就是我设置列标题的方式
DataGridTemplateColumn TextColumn2 = new DataGridTemplateColumn();
FrameworkElementFactory TextBorder2 = new FrameworkElementFactory(typeof(EMSTextCell));
FrameworkElementFactory TextBlock2 = new FrameworkElementFactory(typeof(TextBlock));
ImageTemplate = new DataTemplate();
TextColumn2.CellTemplate = ImageTemplate;
// Setup a Header Template to use for the backup paths column
System.Windows.DataTemplate BackupHeaderTemplate = new System.Windows.DataTemplate();
Binding HeaderBinding = new Binding("BackupPaths[" + index.ToString() + "].BackupDevice");
FrameworkElementFactory MVDeviceColumnHeader = new FrameworkElementFactory(typeof(EMSPlanViewDeviceNew));
MVDeviceColumnHeader.SetValue(EMSPlanViewDeviceNew.HeightProperty, (double)60);
MVDeviceColumnHeader.SetValue(EMSPlanViewDeviceNew.WidthProperty, (double)85);
MVDeviceColumnHeader.SetValue(EMSPlanViewDeviceNew.TheDeviceProperty, HeaderBinding);
BackupHeaderTemplate.VisualTree = MVDeviceColumnHeader;
TextColumn2.HeaderTemplate = BackupHeaderTemplate;
数据集包含BackupPath的列表,其中包含BackupDevice,BackupStatus和BackupLevel属性。在运行时,将填充网格并绘制标题对象,但标题对象未显示正确的数据,只显示默认值,因此告诉我绑定未正确连接到数据集,或者它是这样一种方式,即" UpdateVisuals"对象中的方法不会被调用,因此不会使用正确的数据重绘标题。 我在这里看到的解决方案建议使用HeaderTemplate,但我认为我这样做是因为我不知道还有什么可以尝试让它解雇绑定。
这就是它在运行时的样子
标题行对象应与第一列图像相同。
有人可以建议我还需要做些什么来纠正我的错误。感谢。
public class BackupDataSet
{ 私有字符串_TheAddress; public string TheAddress { get {return _TheAddress; } 设置{_TheAddress = value; } }
private EMSDevices.EMSBasicDevice _TheDevice;
public EMSDevices.EMSBasicDevice TheDevice
{
get { return _TheDevice; }
set { _TheDevice = value; }
}
private BitmapImage _DeviceIcon;
public BitmapImage DeviceIcon
{
get { return _DeviceIcon; }
set { _DeviceIcon = value; }
}
private string _TheIdent;
public string TheIdent
{
get { return _TheIdent; }
set { _TheIdent = value; }
}
private string _TheMainSignalPath;
public string TheMainSignalPath
{
get { return _TheMainSignalPath; }
set { _TheMainSignalPath = value; }
}
public string BackupPathsCount
{
get
{
if (CountGoodBackupPaths() > 0)
return CountGoodBackupPaths().ToString();
else if (CountLowBackupPaths() > 0)
return (-1 * CountLowBackupPaths()).ToString();
else
return "--";
}
}
private List<EMSDevices.EMSBackupPath> _BackupPaths;
public List<EMSDevices.EMSBackupPath> BackupPaths
{
get { return _BackupPaths; }
set { _BackupPaths = value; }
}
public void AddBackupPath(EMSDevices.EMSBasicDevice backupDevice, EMSDevices.SignalSetting backupStatus, int backupLevel)
{
EMSDevices.EMSBackupPath NewBackupPath = new EMSDevices.EMSBackupPath(backupDevice, backupStatus, backupLevel);
_BackupPaths.Add(NewBackupPath);
}
public BackupDataSet(string address, BitmapImage icon, string ident, string mainsignalpath)
{
_TheAddress = address;
_DeviceIcon = icon;
_TheIdent = ident;
_TheMainSignalPath = mainsignalpath;
_BackupPaths = new List<EMSDevices.EMSBackupPath>();
}
public BackupDataSet(EMSDevices.EMSBasicDevice device)
{
_TheAddress = device.TheAddress;
_TheDevice = device.Clone();
_TheMainSignalPath = device.TheMainSignalPath;
_BackupPaths = new List<EMSDevices.EMSBackupPath>();
}
public int CountGoodBackupPaths()
{
List<EMSDevices.EMSBackupPath> Paths = new List<EMSDevices.EMSBackupPath>();
Paths = (from BackupPath in _BackupPaths
where BackupPath.BackupStatus == EMSDevices.SignalSetting.GoodSignal
select BackupPath).ToList();
return Paths.Count;
}
public int CountLowBackupPaths()
{
List<EMSDevices.EMSBackupPath> Paths = new List<EMSDevices.EMSBackupPath>();
Paths = (from BackupPath in _BackupPaths
where BackupPath.BackupStatus == EMSDevices.SignalSetting.LowSignal
select BackupPath).ToList();
return Paths.Count;
}
}
这是填充数据集的方式:
if (AllCIEDevices.Count > 0)
{ CIEDataSet = new List();
foreach(EMSDevices.EMSBasicDevice dev1 in AllCIEDevices)
{
BackupDataSet dataset = new BackupDataSet(dev1);
foreach(EMSDevices.EMSBackupPath backup in dev1.BackupPaths)
{
dataset.AddBackupPath(backup.BackupDevice, backup.BackupStatus, backup.BackupLevel);
}
CIEDataSet.Add(dataset);
}
// Setup the grid for devices that have the CIE as the parent
PanelDeviceView.ItemsSource = CIEDataSet;
SetupDataGrid(PanelDeviceView, Headers);
}
因此,DataGrid已经通过PanelDeviceView.ItemSource连接了数据集。
每行数据都有一个设备,一些其他参数和一个BackupPaths列表。
答案 0 :(得分:0)
将Source
的{{1}}属性设置为定义Binding
属性的对象:
BackupPaths