BindingSource / BindingNavigator:如何防止编辑绑定的数据源?

发布于 2024-08-21 09:10:45 字数 790 浏览 4 评论 0原文

我使用 VB.NET 和 Visual Studio 2005 创建了一个数据源。我将数据源拖到我的对话框中,VS 使用我的链接对象(System.Windows.Forms.BindingSourceSystem.Windows.Forms.BindingSource)的成员创建了文本框。 code> 和一个 System.Windows.Forms.BindingNavigator

我填充列表(称为 myList),将 myList 设置为 BindingSource 中的数据源,除了我希望它是只读的之外,一切工作都很顺利。如果用户在其中一个文本框中更改某些内容,它会保存更改。

我尝试创建一个只读集合来绑定到 BindingSource,但这并没有解决问题:

Dim detailsDlg As New dlgMyDetails
Dim readOnlyList As New ReadOnlyCollection(Of MyObjects)(myList)

detailsDlg.MyBindingSource.DataSource = readOnlyList
detailsDlg.ShowDialog()

我想我可以禁用所有文本框,但这似乎有点严厉,而且我可能想要更改字体颜色,使其更易于阅读。

理想情况下,我可能不会关心用户是否能够将焦点设置到文本框,甚至编辑内容,但我只是不希望任何更改持续存在。也就是说,如果有人编辑了某些内容,使用导航器转到下一条记录,然后返回,我希望它保持用户播放之前的样子。

有什么建议/指导吗?

提前致谢!

I created a Data Source with VB.NET and Visual Studio 2005. I dragged the data source onto my dialog, and VS created the text boxes with the members of my linked Object, a System.Windows.Forms.BindingSource and a System.Windows.Forms.BindingNavigator.

I populate the List (called myList), set myList as the DataSource in the BindingSource, and things work peachy except for the fact that I want this to be read-only. If the user changes something in one of the text boxes, it saves the changes.

I tried creating a read-only collection to bind to the BindingSource, but that didn't solve the problem:

Dim detailsDlg As New dlgMyDetails
Dim readOnlyList As New ReadOnlyCollection(Of MyObjects)(myList)

detailsDlg.MyBindingSource.DataSource = readOnlyList
detailsDlg.ShowDialog()

I guess I could disable all of the text boxes, but that seems a bit heavy-handed, plus I'd probably want to change the font color so that it's easier to read.

Ideally, I probably wouldn't care if users were able to set focus to the text boxes, or even edit the contents, but I just wouldn't want any changes to persist. That is, if someone edited something, used the navigator to go to the next record, and then returned, I'd want it as it was before the user played with it.

Any suggestions / guidance?

Thanks in advance!

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

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

发布评论

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

评论(2

゛清羽墨安 2024-08-28 09:10:45

从模型-视图-控制的角度来看,您想要的约束不是模型或控件,而是视图。视图应该限制屏幕上可编辑的内容。

如果它确实是只读的,为什么不使用只读的用户界面元素,即标签?这样做的原因是为了减少用户的困惑。如果它是一个文本框,则可以合理地期望数据在某个时刻变得可编辑。如果情况并非如此,那么呈现禁用的文本框可能不是呈现正确的 UI 元素,而是如上所述,呈现标签。

From a Model-View-Control perspective, the constraint you want is not on the model or control, but the view. The view should restrict what is editable on the screen.

If it truly is read-only, why not go with a read-only user interface element, ie, a label? The reason you do this is to reduce confusion to the user. If it is a textbox, there is a reasonable expectation that at some point the data becomes editable. If this is not the case, then presenting a disabled textbox is likely not the right UI element to present, rather, as mentioned, a label.

白龙吟 2024-08-28 09:10:45

您可以将类 (MyObjects) 中的属性更改为 ReadOnly,或将属性 System.ComponentModel.ReadOnly(true) 添加到您的属性,而不是创建 ReadOnlyCollection,例如:

class Person
{
    public Person(int id, string name, string address)
    {
        _id = id;
        Name = name;
        Address = address;
    }

    private int _id = 0;
    public int ID { get { return _id; } }

    [System.ComponentModel.ReadOnly(true)]
    public string Name { get; set; }

    public string Address { get; set; }
}

ID 和 Name 将是只读的,抱歉,如果示例位于C#。希望这有帮助。

干杯。

Instead of making a ReadOnlyCollection you can change the property in your class (MyObjects) to ReadOnly or add attribute System.ComponentModel.ReadOnly(true) to your property, example:

class Person
{
    public Person(int id, string name, string address)
    {
        _id = id;
        Name = name;
        Address = address;
    }

    private int _id = 0;
    public int ID { get { return _id; } }

    [System.ComponentModel.ReadOnly(true)]
    public string Name { get; set; }

    public string Address { get; set; }
}

ID and Name is going to be readonly, sorry if the example is in C#. Hope this helps.

Cheers.

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