使用 CSLA 禁用 C# .NET 中的文本框

发布于 2024-07-13 15:54:24 字数 991 浏览 4 评论 0原文

我正在尝试禁用多个用于在我的用户控件之一中显示数据(而不是编辑)的文本框。 但是,由于某种原因,我无法正确禁用文本框。

我已将“ApplyAuthorization on readWriteAuthorization”设置为 true,并且文本框已数据绑定到正确的属性。

我还在我的对象的 CanWriteProperty 中添加了以下几行:

if (propertyName == OpeningDateProperty.Name) return false;
if (propertyName == ChangeDateProperty.Name) return false;
if (propertyName == CloseDateProperty.Name) return false;
return base.CanWriteProperty(propertyName);

我无法弄清楚我在这里做错了什么。 我最近在其他 UserControls 中实现了几乎相同的功能,没有任何问题...

我在 C# .NET (Visual Studio 2008) 中使用 Windows 窗体

编辑: 代码片段和属性已被采用来自我的客户对象。 该日期代表客户帐户的开设、最后更改和关闭。 它们根本不应该被编辑,事实上在旧的解决方案中它们由 textLabels 表示,但是我们现在想要使用文本框并使属性的 CanWriteProperty 为 false。

我意识到这些信息可能有点稀缺,但我正在寻找在此过程中我可能忘记的内容。

编辑:我们也在使用CSLA,我想(我对这整件事都是新手)这与我们的原因有关想这样做。

编辑(解决方案):正如您在下面的回答中看到的,问题是我没有像我应该设置的那样设置 CurrentItemChanged 事件。

I am trying to disable a number of text boxes intended for displaying data (not edit) in one of my UserControls. However, for some reason I can not get the textBoxes to disable properly.

I've set "ApplyAuthorization on readWriteAuthorization" to true and the textBoxes are databound to the correct properties.

I've also added the following lines to the CanWriteProperty of my object:

if (propertyName == OpeningDateProperty.Name) return false;
if (propertyName == ChangeDateProperty.Name) return false;
if (propertyName == CloseDateProperty.Name) return false;
return base.CanWriteProperty(propertyName);

I can't figure out what I'm doing wrong here. I've implemented pretty much the same thing recently in other UserControls without any problems...

I am using Windows Forms in C# .NET (Visual Studio 2008)

EDIT: The code snippets and the properties are taken from my customer object. The date represent opening, last change and closure of the customer account. They are never supposed to be edited at all and in fact in the old sollution they are represented by textLabels, however we now want to use a text box and make the property's CanWriteProperty false.

I realise that the information might be sort of scarce, but I am looking for what I might have forgotten in this process.

EDIT: We are using CSLA as well and I guess (I'm new at this whole thing) this has something to do with why we want to do it like this.

EDIT (Sollution): As you can see in my answer below, the problem was that I had not set up the CurrentItemChanged event like I should have.

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

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

发布评论

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

评论(4

随遇而安 2024-07-20 15:54:24

如果您尝试将它们设为只读,只需将 .ReadOnly 属性设置为 true 即可。

或者,如果您从未使用这些文本框进行编辑,那么也许只需使用标签即可?

编辑:啊,这看起来更像是一个 CSLA 框架问题,而不是一个纯粹的 Windows 窗体问题。 在这个问题之前我从未听说过 CSLA,但它看起来很有趣。

If you're trying to get them to be read only, then just set the .ReadOnly property to true.

Alternatively, if you're never ever using these textboxes for editing, then maybe just use a Label instead?

EDIT: Ahh it appears this more of a CSLA-framework question than a pure windows forms question. I've never even heard of CSLA before this question, but it looks interesting.

单身情人 2024-07-20 15:54:24

如果要将数据绑定到控件的属性,只需将文本框的“ReadOnly”属性绑定到业务对象的“CanWrite”属性。

If you are databinding to properties of the control just bind the "ReadOnly" property of the textbox to the "CanWrite" property of your business object.

飘逸的'云 2024-07-20 15:54:24

我认为你的意思是只读属性

i think you mean ReadOnly property

感性不性感 2024-07-20 15:54:24

要完成此工作,您需要执行以下操作:

  1. 确保 TextBox 以正确的方式将数据绑定到正确的属性

  2. Set在根对象的 CanWriteProperty 覆盖中对每个文本框进行所需的检查

    if (propertyName == OpeningDateProperty.Name) return false; 
      
  3. 确保 rootBindingsource 的 CurrentItemChanged 事件设置正确

    private void rootBindingSource_CurrentItemChanged(对象发送者,EventArgs e) 
      { 
          readWriteAuthorization1.ResetControlAuthorization(); 
      } 
      
  4. 确保 texBox 的“ApplyAuthorization on ReadWriteAuthorization”设置为 true

这为我解决了问题。

To make this work you need to do the following:

  1. Make sure the TextBox is databound to the right property in the correct way

  2. Set up the needed checks for each textBox in the CanWriteProperty override in your root object

    if (propertyName == OpeningDateProperty.Name) return false;
    
  3. Make sure the rootBindingsource's CurrentItemChanged event is set up right

    private void rootBindingSource_CurrentItemChanged(object sender, EventArgs e)
    {
        readWriteAuthorization1.ResetControlAuthorization();
    }
    
  4. Make sure the texBox's "ApplyAuthorization on ReadWriteAuthorization" is set to true

This solved the problem for me.

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