JavaFX-CSS: How to “move” style of parent to a child?

妖精的绣舞 提交于 2019-12-04 13:16:30

The reason why it doesn't work is because .label doesn't have a rule where -fx-background is applied, so any value assigned to it won't affect any of the Label properties.

What's more, Label doesn't use a -fx-background-color property.

So an easy solution would be adding it:

.tree-text-only > .label {
    -fx-background-color: -fx-background; 
}

Now you can apply all the style using only this "semantic" colors.

Note I've added also the case where the control doesn't have the focus:

/* 
 * remove the highlight from cell as a whole 
 */

/* Selected rows */
.tree-view:focused > .virtual-flow > .clipped-container > .sheet > .tree-text-only:filled:selected {
    -fx-background: -fx-control-inner-background; 
}
/* Selected when control is not focused */
.tree-text-only:filled:selected {
    -fx-background: -fx-control-inner-background; 
}
/* focused cell (keyboard navigation) */
.tree-view:focused > .virtual-flow > .clipped-container > .sheet > .tree-text-only:focused {
    -fx-cell-focus-inner-border: -fx-control-inner-background; 
}

/* 
 * highlight only the label
 */

// Add background color rule
.tree-text-only > .label {
    -fx-background-color: -fx-background; 
}
/* Selected rows */
.tree-view:focused > .virtual-flow > .clipped-container > .sheet > .tree-text-only:filled:selected > .label {
    -fx-background: -fx-selection-bar; 
}
/* Selected when control is not focused */
.tree-text-only:filled:selected > .label {
    -fx-background: -fx-selection-bar-non-focused;
}
/* focused cell (keyboard navigation) */
.tree-view:focused > .virtual-flow > .clipped-container > .sheet > .tree-text-only:focused > .label {
    -fx-background: -fx-selection-bar; 
}

Again, I'm not 100% sure I understand your requirements, but following quick test works for me (Label is assigned to a graphic property of a Button):

.button {
    -fx-text-fill: red;
    -fx-background-color: yellow;
}

.button > .label {
    -fx-text-fill: blue;
    -fx-background-color: yellow;
}

.button:pressed > .label {
    -fx-text-fill: white;
    -fx-background-color: black;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!