【发布时间】:2011-08-31 22:44:36
【问题描述】:
我对 xaml 很陌生,我正在尝试从自定义类绑定属性以应用主题系统,但 xaml 尝试在设置我的属性之前进行绑定。我也无法让 INotifyPropertyChanged 与我的自定义类一起使用,我希望允许用户即时切换主题。
我的(简化的)代码:
public class clsThemeObjects : DependencyObject, ComponentModel.INotifyPropertyChanged
{
public event PropertyChangedEventHandler System.ComponentModel.INotifyPropertyChanged.PropertyChanged;
public delegate void PropertyChangedEventHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e);
public SolidColorBrush ButtonTextBrush {
get {
return (SolidColorBrush)GetValue(ButtonTextBrushProperty);
}
set {
SetValue(ButtonTextBrushProperty, value);
if (PropertyChanged != null) {
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("ButtonTextBrush"));
}
}
}
public static readonly DependencyProperty ButtonTextBrushProperty = DependencyProperty.Register("ButtonTextBrush", typeof(SolidColorBrush), typeof(cls_Globals), new PropertyMetadata(null));
}
static class Globals
{
static ThemeObjects = new clsThemeObjects();
public Globals()
{
ApplyTheme();
}
Public static void ApplyTheme()
{
ThemeObjects.ButtonTextBrush = new SolidColorBrush(Colors.Black);
}
}
在我的 xaml 中,我正在这样做:
<Button Style="{StaticResource ButtonBlankStyle}" Grid.Row="2" Name="btnAddModules" HorizontalContentAlignment="Stretch">
<Border BorderThickness="2" BorderBrush="Black" CornerRadius="2" Margin="0,4">
<TextBlock Text="Test" Foreground="{Binding Path=ButtonTextBrush, Source={StaticResource ThemeObjects}}" />
</Border>
</Button>
我认为使用 iNotifyPropertyChanged 会起作用,但它不起作用,有人可以帮忙吗?
谢谢
【问题讨论】:
标签: silverlight binding inotifypropertychanged