【发布时间】:2018-02-28 01:49:56
【问题描述】:
听起来很像其他关于绑定标题的问题 - 但通常它们是关于简单的标题,如 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;
Dataset 包含具有 BackupDevice、BackupStatus 和 BackupLevel 属性的 BackupPaths 列表。在运行时,填充了网格并绘制了标题对象,但是标题对象没有显示正确的数据,只是默认值,所以这告诉我绑定没有正确连接到数据集,或者它只是在这样就不会调用对象中的“UpdateVisuals”方法,因此不会用正确的数据重绘标题。 我在这里看到的解决方案建议使用 HeaderTemplate,但我认为我正在这样做,所以我不确定还有什么可以尝试让它触发绑定。
这是它在运行时的样子
标题行对象应与第一列图像相同。
谁能建议我还需要做什么来纠正我的错误。谢谢。
public class BackupDataSet
{ 私人字符串_TheAddress; 公共字符串 TheAddress { 得到 { 返回 _TheAddress; } 设置 { _TheAddress = 值; } }
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 属性在哪里定义?
-
BackupPaths 是一个名为 BackupDataSet 的类的属性:
-
这个类是如何以及在哪里使用的?
-
私人列表
_BackupPaths;公共列表 BackupPaths { get { return _BackupPaths; } 设置 { _BackupPaths = 值; } } -
这里有人有什么想法吗?或者这是一个死鸭子,我需要找到另一种方法来解决这个问题?