简体   繁体   English

绑定到静态类的静态属性

[英]Bind to static property of static class

I'm searching for a way to bind the ItemsSource of a GridView to the static property of a static class . 我正在寻找将GridViewItemsSource绑定到静态类static属性的方法。

Since the class is static I can't set the (Binding).Source to an instance of the class and setting it to the property doesn't work. 由于该类是静态的 ,因此无法将(Binding).Source设置为(Binding).Source的实例并将其设置为该属性不起作用。

Unfortunately the XAML markup extensions x:Static doesn't work for Windows Apps so that's not an option either. 不幸的是,XAML标记扩展名x:Static不适用于Windows Apps,因此也不是一种选择。

Marking this bold since people obviously DO NOT understand that the WPF answer DOES NOT work in Windows Runtime. 标记这个大胆的,因为人显然明白WPF答案无法在Windows运行时的工作。 How about reading the question before marking it as duplicate... 在将问题标记为重复之前阅读该问题怎么样?

This worked for me with no problems. 这对我毫无问题。

public static class StaticClass 
{ 
    public static ObservableCollection<string> StaticStrings = new ObservableCollection<string>(); 
} 

private void ComboBox_Loaded(object sender, RoutedEventArgs e) 
{ 
    for (int i = 0; i < 5; i++) 
    { 
        StaticClass.StaticStrings.Add("static" + i.ToString()); 
    } 
    this.DataContext = StaticClass.StaticStrings; 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM