Silverlight试图在属性设置之前绑定

时间:2011-08-31 22:44:36

标签: silverlight binding inotifypropertychanged

我对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会有效,但事实并非如此,有人可以帮忙吗?

由于

1 个答案:

答案 0 :(得分:0)

如果声明了static ThemeObjects = new clsThemeObjects();在静态类中,它并不意味着您可以使用StaticResource标记扩展从XAML访问它。您应该在XAML中将其声明为资源来执行此操作。请详细了解StaticResource