(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;
});
2 Replies
when it tries to modify the control properties on the ui thread with the this.dispatcher part it throws me this exception
also if i just simply do
txtPrimaryAmmo.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00FF00"));
in the dispatcher it works just fine
apparently all i had to do was .freeze() the SolidColorBrush variable
still dont know why though, in the dispatcher i have several UI property changes happening with strings,margins,visibilities and none of those caused this error