绑定路径属性应设置为什么?

发布于 2024-07-22 21:03:42 字数 921 浏览 2 评论 0原文

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

    public struct TimeSlotInfo
    {
        public int TimeSlotID;
        public int StartMin;
        public int CalcGridColumn;
        public string BackgroundCol;
        public bool ToDisable;
    }

并且我有一个 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
};

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

lstBox.ItemsSource = TimeSlotsInfo;

现在,我如何设置绑定路径来引用上面的“BackgroundCol”字段查询结果?

我尝试过 {Binding Path=TimeSlotInfo.BackgroundCol}、{Binding Path=TimeSlotInfo/BackgroundCol},最后是 {Binding Path=BackgroundCol}...它们似乎都不起作用..

任何人都可以帮忙吗? 我已尝试尽可能简化示例。 希望我的问题足够清楚。 提前致谢。

Assuming I have this struct definition in C#:

    public struct TimeSlotInfo
    {
        public int TimeSlotID;
        public int StartMin;
        public int CalcGridColumn;
        public string BackgroundCol;
        public bool ToDisable;
    }

And I have a linq query as so:

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
};

If i set the ItemsSource property of say a ListBox as below:

lstBox.ItemsSource = TimeSlotsInfo;

Now, how do i set a binding path to reference the "BackgroundCol" field from the above query result?

I've tried {Binding Path=TimeSlotInfo.BackgroundCol}, {Binding Path=TimeSlotInfo/BackgroundCol}, and finally {Binding Path=BackgroundCol}...none of them seem to be working..

Can anyone help? I've tried to simplify the example as possible. Hope my problem is clear enough.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半窗疏影 2024-07-29 21:03:42

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

class TimeslotInfo {
    public int TimeslotId {get; set;}
    /* Etc... */
}

The last one is correct ({Binding Path=BackgroundCol}) - however, you can't bind to fields, you can only bind to Properties. Define your class to be:

class TimeslotInfo {
    public int TimeslotId {get; set;}
    /* Etc... */
}
柠北森屋 2024-07-29 21:03:42

您不仅应该像 Paul 所说的那样使用属性进行绑定,而且一般来说您应该避免公共字段

另外,这感觉不应该是一个结构体——你真的想要值类型语义吗? 如果有疑问,您应该默认在 C# 中创建类 - 您真正很少需要一个结构体。

最后,即使您确实想要一个结构,您也几乎应该始终使结构不可变。 几乎可以肯定,可变结构会产生意想不到的结果。 这一切都定义良好并且有充分的理由,但这可能不是您期望的行为。 可变结构是邪恶的。

Not only should you use properties for binding as Paul says, but in general you should avoid public fields in the first place.

In addition, this doesn't feel like it should be a struct - do you really want value-type semantics? When in doubt, you should default to creating classes in C# - it's pretty rare that you really want a struct.

Finally, even if you did want a struct, you should almost always make structs immutable. You're almost certain to have unexpected results from mutable structs. It's all well defined and for good reasons, but it's probably not the behaviour you expect. Mutable structs are evil.

苍白女子 2024-07-29 21:03:42

如果您希望项目显示BackgroundCol 中存储的任何内容,只需将ListBox 上的DisplayMemberPath 属性设置为“BackgroundCol”即可。 如果这不是您想要实现的目标,请更具体。

希望这可以帮助!!

If you want your items to display whatever is stored in BackgroundCol, you can just set the DisplayMemberPath property on your ListBox to "BackgroundCol". If this isn't what you're trying to achieve, please be more specific.

Hope this helps!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文