我是否必须通过WPF中的接口实现所有usercontrol属性?

时间:2022-12-02 20:20:55

I'm programming a bunch of WPF usercontrols (not CommonControls!) using an Interface for the common properties like the alignments or the Content (depending on the control; TextBox returns the Text, and Label the Content per example). Now i get to controls like the Checkbox, which have the not so common property IsChecked. Do i have to implement this property by the Interface or is it allowed to program it in the usercontrol itself?

我正在编写一堆WPF用户控件(不是CommonControls!),使用Interface作为常用属性,例如对齐或内容(取决于控件; TextBox返回Text,并为每个示例标记内容)。现在我得到像Checkbox这样的控件,它们具有不常见的属性IsChecked。我是否必须通过接口实现此属性,还是允许在usercontrol本身中对其进行编程?

2 个解决方案

#1


0  

I hope you're talking about binding a property to a view model or directly mentioning the property value in the xaml itself.

我希望您谈论将属性绑定到视图模型或直接提及xaml本身的属性值。

It actually depends upon your requirement. We normally bind a property value to a view model or code behind when that property needs to be checked by certain conditions and then to be set. If you're sure of the property's value, you can set it in xaml itself.

这实际上取决于您的要求。我们通常将属性值绑定到视图模型或后面的代码,当需要通过某些条件检查该属性然后进行设置。如果您确定属性的值,可以在xaml中设置它。

For example if you want to set a textbox's IsEnabled property, and you're sure that the text box is always editable. Then set it as true in xaml itself. But if you're text box need to be enabled during an event is handled, bind it to a property in view model and set it to true during the event trigger.

例如,如果要设置文本框的IsEnabled属性,并确保文本框始终可编辑。然后在xaml中将其设置为true。但是,如果在处理事件期间需要启用文本框,请将其绑定到视图模型中的属性,并在事件触发期间将其设置为true。

#2


0  

Okay, i have to guess it was a dumb question. But after eight hours you don't know anymore, if you're Hillary or Donald.

好吧,我不得不猜这是一个愚蠢的问题。但是八小时后你就不知道了,如果你是希拉里或唐纳德。

The answer is: An Interface can inherit from another and implement all members from the "parent" interface.

答案是:接口可以从另一个继承并实现“父”接口中的所有成员。

#1


0  

I hope you're talking about binding a property to a view model or directly mentioning the property value in the xaml itself.

我希望您谈论将属性绑定到视图模型或直接提及xaml本身的属性值。

It actually depends upon your requirement. We normally bind a property value to a view model or code behind when that property needs to be checked by certain conditions and then to be set. If you're sure of the property's value, you can set it in xaml itself.

这实际上取决于您的要求。我们通常将属性值绑定到视图模型或后面的代码,当需要通过某些条件检查该属性然后进行设置。如果您确定属性的值,可以在xaml中设置它。

For example if you want to set a textbox's IsEnabled property, and you're sure that the text box is always editable. Then set it as true in xaml itself. But if you're text box need to be enabled during an event is handled, bind it to a property in view model and set it to true during the event trigger.

例如,如果要设置文本框的IsEnabled属性,并确保文本框始终可编辑。然后在xaml中将其设置为true。但是,如果在处理事件期间需要启用文本框,请将其绑定到视图模型中的属性,并在事件触发期间将其设置为true。

#2


0  

Okay, i have to guess it was a dumb question. But after eight hours you don't know anymore, if you're Hillary or Donald.

好吧,我不得不猜这是一个愚蠢的问题。但是八小时后你就不知道了,如果你是希拉里或唐纳德。

The answer is: An Interface can inherit from another and implement all members from the "parent" interface.

答案是:接口可以从另一个继承并实现“父”接口中的所有成员。