Whenever dealing with a BindableProperty, there are 4 different binding modes:
TwoWay — data goes both ways between source and target
OneWay — data goes from source to target
OneWayToSource — data goes from target to source
OneTime — data goes from source to target, but only when the BindingContext changes
And as per the documentation says:
The default binding mode for most properties such as Rotation, Scale, and Opacity is OneWay.
However, anytime I read the documentation of a specific control and its bindable properties, I'm not able to find about their default binding mode (e.g. Label TextProperty)
Question is: how do I easily get the default binding mode of a control bindable property ?
As the Binding mode docs say:
Most bindable properties have a default binding mode of OneWay but
some properties have a default binding mode of TwoWay, including the
following:
Date property of DatePicker
Text property of Editor, Entry, SearchBar, and EntryCell
IsRefreshing property of ListView
SelectedItem property of MultiPage
SelectedIndex and SelectedItem properties of Picker
Value property of Slider and Stepper
IsToggled property of Switch
On property ofSwitchCell
Time property of TimePicker
If there are control binding modes you are not sure about, you can check the source code of Maui to determine, for example, the BindingMode of ValueProperty of Slider is TwoWay.
Related
I wrote a UILabel-subclass called Badge that is supposed to display notifications similar to Apple's icon-badges. I made Badge customizable in Interface Builder by prefixing it with #IBDesignable and its properties with #IBInspectable. The setup works both from code and IB, where it is rendering properly. The only thing I'm struggling with is setting up new default-values for the inherited #IBDesignable-properties such as text and backgroundColor. I have tried to set these values in the init-methods and also prepareForInterfaceBuilder(), but the values in the Attributes Inspector in the UILabel and UIView-section remain unchanged.
Here my question:
Is it possible to change/override the UILabel's default text found in the Attributes Inspector so that a custom text instead of "Label" is rendered in IB for my subclass by default?
I would like to bind the boolean enabled property of an NSTextField to the state of an NSButton. I already tried adding a custom NSValueTransformer that transforms the state of the NSButton into NSNumber. However, in that scenario the text fields are disabled all the time for some reason. My second approach: To bad fails also since NSValueTransformer does not offer return primitives types such as BOOL.
Example:
The screenshot shows an example in which the text fields are disabled because the checkbox has the state NSOnState. I also would like to bind the labels to this state.
Further, it would be convenient, if I could set a "disabled text" in Interface Builder. In the above example I set the text in the associated class.
Edit:
I set self.anonymousLoginCheckbox.state as the Model Key Path for the enabled property of the account text field. Similar for the password text field. However, it does not work.
Update:
I created an example project available on GitHub showing the implementation kindly described by Nicolas Bachschmidt.
NSButton isn't KVO compliant for the key state. Cocoa Bindings require the observed object to emit notifications when the observed property changes. As NSButton's state is just a wrapper for its cell's state, -[NSButton setState:] method (and the automatic KVO notifications) isn't invoked when the user click the button (but -[NSCell setState:] is). If you set the model key path to self.anonymousLoginCheckbox.cell.state, it will work.
How do I bind the enabled state of an NSMenuItem to an NSArrayController's selection? I've tried binding the item's enabled state to the controller's selectedObjects or selectedIndexes and in neither case is the menuitem ever enabled when there are selections. In IB, I unchecked the "enable" checkbox. I simply want the NSMenuItem to be enabled when there are selections in the table. My table allows for multiple selection and I also use a button which is bound to selectedObjects.#count and the button enables/disables as expected, so I thought using the same keypath would work for the menuitem as well, but nope. This can't be difficult as I can't find an answer via google, so I figure it must be simple.
Thanks
The enabled binding has to get a BOOL value, and unfortunately won't just treat any old object as a boolean True. Fortunately, however, NSValueTransformer makes it easy to do so. There's a couple of constants named in the NSValueTransformer Class Reference which you can use in the bindings pane in IB.
In your case, you can bind the Model Key Path to "selectedObjects" and enter "NSIsNotNil" in the Value Transformer field. The transformer gives the binding the BOOL value it needs.
I'm trying to create a simple application that draws a grid in a custom view.
The custom view size is fixed (it doesn't depend on the size of the window).
The custom view is embedded in a scroll view to be able to explore the grid when the scroll view can't display the entire custom view.
Now i want to add sliders for controlling the grid parameters (nb raws, nb columns, tile width, tile height, ...), and these parameters influence the size of the custom view.
As an experiment, i'm trying to bind one slider's value to the width of my custom view but fail to find a way to do it.
How am i supposed to do this sort of things ?
Is it possible to do it in InterfaceBuilder ? I expected to find a width binding in Bindings Inspector Window but it's not there, curiously ;-)
Thanks.
You can't bind the width of a plain NSView, and binding to a property of a view is always a bad idea. View properties are seldom observable. Moreover, there is no width property; it's one member of the structure that is the value of the frame property, which you must set all at once or not at all.
As for exposing bindings in your custom view, you can do that, provided you keep the properties observable (which consists of little more than only changing the property's value using its setter). You'll need to expose the bindings in your view class's initialize method, and you'll need to write an IBPlugin.
See also the Cocoa Bindings Programming Topics.
I know that component-model indicates whether a property has a default value or not, by means of ShouldSerializeValue method of PropertyDescriptor.
The base windows-forms Control class, has some properties like ForeColor, BackColor and Font, that defaults to the same value of the parent, but I could not find any TypeDescriptor or PropertyDescriptor that provides these default values. The Control class does not implement ICustomTypeDescriptor nor has a TypeDescriptionProviderAttribute.
How does the Control class indicates that these properties should be serialized or not?
Where does it provide the PropertyDescriptors for these properties??
Thanks!!!
They don't have default values. These properties are "ambient" properties. The Control class detects that a property assignment has occurred for them. If that never happened, it uses the corresponding property from the Parent. Which is nice, it ensures child controls use the same colors and font as their container.
There is a ShouldSerializeForeColor() method in the Control class. It is internal and can't be overridden by user code. Same for the other properties. Have a look-see with Reflector or the .NET Reference Source.
The MSDN Library documents them like this:
Windows Forms controls use ambient
properties so child controls can
appear like their surrounding
environment. An ambient property is a
control property that, if not set, is
retrieved from the parent control. If
the control does not have a Parent,
and the property is not set, the
control attempts to determine the
value of the ambient property through
the Site property. If the control is
not sited, if the site does not
support ambient properties, or if the
property is not set on the
AmbientProperties, the control uses
its own default values. Typically, an
ambient property represents a
characteristic of a control, such as
BackColor, that is communicated to a
child control. For example, a Button
will have the same BackColor as its
parent Form by default. Ambient
properties provided by the Control
class include: Cursor, Font,
BackColor, ForeColor, and RightToLeft.