user4388177 user4388177 - 2 months ago 6
Linux Question

Change the theme at runtime

I have a GTK# 3 application under Linux and I would like to force a theme selected from the application rather than the system one.

Is there a way to achieve this?

Uno Uno

GTK# 3 can load a CSS theme at runtime and you can recursively apply it to all the children of a widget.

var cssProvider = new CssProvider();


ApplyCssRecursively(window, cssProvider, UInt32.MaxValue);

static void ApplyCssRecursively(Widget widget, IStyleProvider provider, uint priority)
    widget.StyleContext.AddProvider(provider, priority);

    var container = widget as Container;

    if (container == null) return;

    foreach (var child in container.Children)
        ApplyCssRecursively(child, provider, priority);