我想在 C# 中绑定 Border.BackgroundProperty。绑定有效,但 Border-Element 没有在更改时更新。
UIElement(fb_CoonetionIndicator = 边框):
var indicatorElement = new InputElement("");
indicatorElement.InputElementType = InputElementTypeEnum.Indicator;
// verified label binding
Binding indicatorBinding = new Binding("VerificationText");
indicatorBinding.Source = ApplicationPersistantDataModel.Instance;
indicatorElement.local_indicatorlabel.SetBinding(Label.ContentProperty, indicatorBinding);
// backgroundcolor binding
Binding colorBinding = new Binding("VerificationColor");
colorBinding.Source = ApplicationDynamicDataModel.Instance;
colorBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
fb_ConnectionIndicator.fb_backGroundBorder.SetBinding(Border.BackgroundProperty, colorBinding);
fb_ConnectionIndicator.AddElementToStackPanel(indicatorElement);
数据源:
public sealed class ApplicationDynamicDataModel
{
private static ApplicationDynamicDataModel instance = new ApplicationDynamicDataModel();
public static ApplicationDynamicDataModel Instance
{
get { return instance; }
set { instance = value; }
}
// verification indicator color
private SolidColorBrush _verificationColor = new SolidColorBrush(Colors.GreenYellow);
public SolidColorBrush VerificationColor
{
get { return _verificationColor; }
set
{
_verificationColor = value;
OnPropertyChanged("VerificationColor");
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}