Selectors look very similar to CSS. What selectors are available in Avalonia? What is the meaning of this selector for example:
<Style Selector="ListBoxItem:selected /template/ ContentPresenter">
<Setter Property="Background" Value="#ffffff" />
</Style>
Yes, selectors are very similar to CSS. Currently (at 0.5.0 alpha) the following selectors are available. I'm explaining these selectors by example, as otherwise it gets hard to understand:
Button: This selects a control by type, so if you write a selector Button then it will match the Avalonia.Controls.Button class. It won't match derived types [1]
:is(Button): This is very similar to Class except it also matches derived types
Button TextBlock: When two selectors are separated by a space, then the selector will match descendants, so in this case the selector will match any TextBlock that is a logical descendant of Button
Button > TextBlock: When two selectors are separated by a >, then the selector will match children, so in this case the selector will match any TextBlock that is a direct logical child of Button
Button#MyButton this will match any Button with the Name of MyButton
Button.MyClass this will match any Button with the string MyClass as an entry in its Classes
Button:disabled this will match any Button with the :disabled pseudo-class
Button[IsDefault=true]: this will match any Button which has an IsDefault property of true
Button /template/ ContentPresenter this matches a ContentPresenter in the control template of a Button [2]
[1]: Note the type of an object is actually determined by looking at its IStyleable.StyleKey property, so if you do want your control which inherits from Button to be styled as a Button, then you can implement the IStyleable.StyleKey on your class to return typeof(Button).
[2]: This is similar to the (now deprecated) /deep/ operator in CSS; CSS shadow DOM is a very similar concept to control templates.
Related
When inheriting a TextBox, not all styles are respected by the child (see image in appendix).
<TextBox
x:Class="AdvoTools.PerfectTimeNative.UI.Controls.TextInput"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"/>
using Microsoft.UI.Xaml.Controls;
namespace AdvoTools.PerfectTimeNative.UI.Controls
{
public sealed partial class TextInput : TextBox
{
public TextInput() => InitializeComponent();
}
}
I tried different approaches:
Inheriting as above results in certain styles missing;
Copying the hardcoded TextBox styles from generic.xaml and setting their target to the inherited control results into the same effect;
using templated controls as mentioned here does not seem to work either. When I create it through the templated control preset for UWP and change the namespaces from Windows to Microsoft, the control is empty (the generated generic.xaml does not appear to be respected at all. Deleting it altogether makes no difference);
Defining a style with the BasedOn property has the same reuslt as the first approach.
Note:
The blue bottom border thickness does not increase when the TextBox is focused (not visible here);
the corder radius is 0.
This should be a no-brainer but I am really struggling at the moment.
Create a standalone custom class:
public class MyCustomTextBox : TextBox
{
public MyCustomTextBox()
{
DefaultStyleKey = typeof(MyCustomTextBox);
}
}
And add the following default template for it to themes/generic.xaml:
<Style TargetType="local:MyCustomTextBox" BasedOn="{StaticResource DefaultTextBoxStyle}" />
Then it should look like a default TextBox.
I am trying to create a non-modal (modeless) QMessageBox window with several buttons and text. To make it modeless, I am giving it the MainWindow object as its parent (which inherits from QMainWindow).
For example:
class MainWindow(QMainWindow):
...
def create_popup(self):
message_box = QMessageBox(self)
# add buttons, text, etc.
message_box.setModal(False)
message_box.show()
However, the issue arises that I am providing the styling of my MainWindow through a stylesheet, built from a .ui file (using PyQt5.uic.loadUi('file', main_window)). In the .ui file, I am specifying the stylesheet of the MainWindow as having background-color: black. Because the QMessageBox I create inherits from MainWindow, it also inherits its stylesheet, making both the background of the box black and that of the buttons.
I know I could manually style the box, but I would like to be able the remove the style applied by the parent to the children, while still maintaining the ability to use the QMessageBox as a modeless pop-up.
If I remove the parent of the QMessageBox (so I do message_box = QMessageBox(None)), calling show() does nothing - I have to use exec_(). This does result in the expected presentation of the box with the default styling, however.
If I try to manually override the background-color attribute (with message_box.setStyleSheet("background-color: grey;")), this messes up the style of the buttons, and makes them into rectangular boxes (since the buttons also inherit from the QMessageBox).
I would like to make it so that either 1) the style applied to my MainWindow is not inherited by the QMessageBox, 2) the QMessageBox can be modeless without being a child of the MainWindow, or 3) I can override the styles with the full styles that are default to my platform (including OS and dark mode preferences, like Qt normally does).
In vue it's easy to change the class of an element dynamically. However, what I want to do has nothing to do with that:
I have an attribute color with the value #333 for example. Now I want to build a css class like:
.myColorClass > a {
color: this.color;
}
It's not possible for me to style every single element a individually with the style attribute. So how do I approach this problem with Vue?
I have this statement inside my QML item:
Rectangle {
// ...
anchors.right: someItemID.right
// ...
}
I'm receiving this warning for my Rectangle item:
QML Rectangle: Detected anchors on an item that is managed by a layout. This is undefined behavior; use Layout.alignment instead.
How can I use Layout.alignment to resolve the above warning? How can I pass another item ID to Layout.alignment? Is it possibe?
A layout manages the positions and sizes of all of its child items. Using anchors inside child items is not allowed as it could override these rules. You can only influence those properties provided by the layout in the Layout object attached to its children, which is hinted at in the warning message. Layout.alignment controls how the item is aligned within the cell created for it by the layout. You can therefore align an item to the edges of its adjacent cells, but you can't directly anchor to their items by ID.
If you need more precise control, you should position the items outside the layout using position and/or anchor properties.
I want to change selected item background color of my GridView based on pages. I define a Color property in each ViewModels, then assign the ViewModel to the view's DataContext. I edit the ItemContainerStyle in app.xaml and want to bind the ViewModel's color property to the selectionbackground, so that the background color of selected item is different in each views, but it doesn't work, I couldn't see the expected color. Anyone can help?
If we apply styles in the app.xaml page they will apply to all our application.
I think you can do this by defining your resources at page level using "UserControl.Resources" in your page.
<UserControl.Resources>
<Style TargetType="...">
...
</Style>
</UserControl.Resources>
You can even set key property in the style and apply to the control like bellow
<UserControl.Resources>
<Style x:Key="my_key" TargetType="...">
....
</Style>
</UserControl.Resources>