Binding Path属性应该设置为什么?

COD*_*NLY 1 wpf binding path itemssource

假设我在C#中有这个结构定义:

    public struct TimeSlotInfo
    {
        public int TimeSlotID;
        public int StartMin;
        public int CalcGridColumn;
        public string BackgroundCol;
        public bool ToDisable;
    }
Run Code Online (Sandbox Code Playgroud)

我有一个linq查询如下:

var TimeSlotsInfo = 
from ts in datacon.TimeSlots
select new TimeSlotInfo
{
    TimeSlotID = ts.TimeSlotID,
    StartMin = ts.StartMin,
    CalcGridColumn = CalcTimeSlotGridColumn(ts.StartMin),
    BackgroundCol = ts.ColorName,
    ToDisable = false
};
Run Code Online (Sandbox Code Playgroud)

如果我设置ListBox的ItemsSource属性如下:

lstBox.ItemsSource = TimeSlotsInfo;
Run Code Online (Sandbox Code Playgroud)

现在,如何设置绑定路径以引用上述查询结果中的"BackgroundCol"字段?

我试过{Binding Path = TimeSlotInfo.BackgroundCol},{Binding Path = TimeSlotInfo/BackgroundCol},最后{Binding Path = BackgroundCol} ...它们似乎都没有工作......

有人可以帮忙吗?我试图尽可能地简化示例.希望我的问题很清楚.提前致谢.

Ana*_*tts 5

最后一个是正确的({Binding Path = BackgroundCol}) - 但是,您无法绑定到字段,您只能绑定到Properties.将您的班级定义为:

class TimeslotInfo {
    public int TimeslotId {get; set;}
    /* Etc... */
}
Run Code Online (Sandbox Code Playgroud)