C#C
C#2y ago
4 replies
duk

(WPF) Changing controls properties on the UI Thread Problem

Why does this work when i do it before the UI loads
txtPrimaryAmmo.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00FF00"))


And why doesnt this work
SolidColorBrush pAmmoColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFFFF"));

if (primary != null) {
    if (percentage <= 100 && percentage >= 50) {
        pAmmoColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00FF00"));
    } else if (percentage < 50 && percentage >= 25) {
        pAmmoColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF5F15"));
    } else if (percentage < 25) {
        pAmmoColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#CC0000"));
    }
} else {
}

this.Dispatcher .Invoke(() => {
    txtPrimaryAmmo.Foreground = pAmmoColor;
});
Was this page helpful?