Binding in Control with class Attribute
UI5 doesn't support binding for class
in XML view directly as it's not a valid property of ManagedObject
. Nevertheless, there is a workaround by adding custom data:
Add
CustomData
with the propertywriteToDom
to your control. Use your expression binding there:<ControlXYZ class="myControl">
<customData>
<core:CustomData xmlns:core="sap.ui.core"
key="green"
value="foo"
writeToDom="{= expression}"
/>
</customData>
</ControlXYZ>Depending on the outcome of your expression binding,
data-green
will be added to the control's HTML element. The browser can then manipulate the color corresponding to the attribute selector.Your CSS should thus include the selector accordingly:
.myApp .sapControlXYZ.myControl[data-green] { /* ... */ }
Here is an example: https://embed.plnkr.co/LAv1qfsUjX0Anu7S/
Of course, you can also bind anything you want to the value
property of the CustomData
in order to react on more granular CSS selectors. To learn more about how to leverage custom data in DOM, check out the documentation topic Writing Data to the HTML DOM as DATA-* Attribute.
⚠️ Before using custom CSS..
There might be controls which don't require custom CSS. Especially when it comes to styling
sap.m.Text
for example, there is:- Text with semantic colors:
sap.m.ObjectStatus
or.ObjectNumber
. - Text with custom format:
sap.m.FormattedText
.
- Text with semantic colors:
SAP explicitly warns not to use custom CSS styles.
As stated in the Compatibility Rules, the HTML and CSS generated by SAPUI5 is not part of the public API [...]. As such, SAP Fiori launchpad apps should not override styles.
Generally, the importance of adding custom CSS styles should be always questioned and double-checked with stakeholders to improve UI consistency across Fiori apps as well as to reduce the maintenance costs and TCO that would otherwise rise significantly with custom CSS.
Data binding of CSS class attribute in XML view
Unfortunately, it is indeed not possible to bind the class attribute, as Qualiture already mentioned.
However, there is a workaround using custom data, which can be bound, written to the DOM and hence also used for styling purposes.
You can see an example here in the documentation: Writing Data to the HTML DOM as DATA-* Attribute
Hope that helps
How to bind UI color attribute with class property
Try to bind to the State
property of the UserControl
:
<Ellipse Fill="{Binding Path=State,
RelativeSource={RelativeSource AncestorType=UserControl},
Converter={StaticResource BoolToColor}}" StrokeThickness="3" Stroke="Gray"/>
You should also return a Brush
instead of a Color
from your converter:
public class BoolToColor : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool)
{
if ((bool)value == true)
return Brushes.Green; // to replace with onColor
else
return Brushes.Red; // to replace with offColor
}
return Brushes.LightGray;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is Brush)
{
if ((Brush)value == Brushes.Green) // to compare with onColor
{
return true;
}
}
return false;
}
}
Bind different properties of a Class to two instances of the same UserControl
You could add a string dependency property to the UserControl
class (TextBoxUC.xaml.vb):
Public Class TextBoxUC
Public Property Text() As String
Get
Return CType(Me.GetValue(TextProperty), Boolean)
End Get
Set(ByVal value As String)
Me.SetValue(TextProperty, value)
End Set
End Property
Public Shared ReadOnly TextProperty As DependencyProperty = DependencyProperty.Register("Text", GetType(String), GetType(TextBoxUC), New PropertyMetadata(Nothing))
End Class
And bind the TextBox
in the UserControl
(TextBoxUC.xaml) to this one:
<TextBox x:Name="tbx" Text="{Binding Text, RelativeSource={RelativeSource AncestorType=UserControl}}" />
Then you could just bind this dependency property to a property of the Person
DataContext
as usual:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ClassPropertiesToTextBox"
mc:Ignorable="d"
Title="Window1" Height="300" Width="300">
<Window.DataContext>
<local:Person FirstName="Mickey" LastName="Mouse" />
</Window.DataContext>
<StackPanel>
<local:TextBoxUC Text="{Binding FirstName}" />
</StackPanel>
</Window>
Edit: Or you could bind the dependency property programmatically:
p = New Person
p.FirstName = "Homer"
p.LastName = "Simpson"
tbxFirstNameUC = New TextBoxUC
'Set DataContext to my Person class instance'
tbxFirstNameUC.DataContext = p
tbxFirstNameUC.SetBinding(TextBoxUC.NameProperty, New Binding("FirstName"))
Related Topics
Are CSS3 ::Before and ::After Pseudo Elements Supported by IE9 or Not
Select Inputs and Text Inputs in HTML - Best Way to Make Equal Width
How to Make a Fieldset Legend-Style "Background Line" on Heading Text
Differencebetween Outline and Border CSS Properties
How to Reverse the Order of Columns in Bootstrap 4
How to Make Sure Select Option Text Align in the Center in Ie
Does Less Have an "Extend" Feature
3 Columns, Center Column Fixed, Sides Fill Parents
Why Don't Margin-Top: Auto and Margin-Bottom:Auto Work the Same as Their Left and Right Counterparts
Custom.CSS Has Stopped Working in 32.0.1700.76 M Google Chrome Update
Css-Only Sticky Table Headers in Chrome
How to Add Animated Gradient to an Svg Path
Which Are the Most Important Media Queries to Use in Creating Mobile Responsive Design
I Cannot Change the Font Size of Package Explorer in Eclipse
Nesting Pseudo-Elements Inside Pseudo-Elements
How to Create Multiple Borders Around Existing Border of Circle