【问题标题】:Silverlight trying to Bind before properties are setSilverlight 在设置属性之前尝试绑定
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 2010-09-27
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 2010-12-27
      • 2012-06-09
      相关资源
      最近更新 更多