-1

I have a C# .NET 4.5 WPF application, with a public static class. Inside that class I have a public static string. What I want is very simple, I want to bind that static string to a WPF Label, so whenever that string is updated, WPF Label will also get updated.

How can I achieve this with simplest and easiest way

Static class below

public static class GlobalStats
{
    private static void updateValues()
    {
        srGlobalStatics="example";   
    }
    public static string srGlobalStatics = "";
}

I want my WPF Label bound to that particular srGlobalStatics string.

Here's my XAML code:

<Label Name="lblGlobalStats" Content="lblGlobalStats" HorizontalAlignment="Left"
       Margin="10,36,0,0" VerticalAlignment="Top"/>

Isn't this possible ?

I can write a function inside MainWindow.xaml.cs however i don't want to put unnecessary code inside there as much as possible.

Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342

2 Answers2

5

Answer for your first post:

 <Label Name="lblGlobalStats" 
 Content="{Binding Source={x:Static wpfApplication1:GlobalStats.srGlobalStatics}}" 
 HorizontalAlignment="Left" Margin="10,36,0,0" VerticalAlignment="Top"/>

To your second question (raised in comments), assuming you want it all static (as ugly as it is):

public static void updateValues()
{
    srGlobalStatics[0] = "changed";
}

public static ObservableCollection<string> srGlobalStatics = 
   new ObservableCollection<string> { "test" };

Wpf:

<Label Name="lblGlobalStats" 
    Content="{Binding Path=[0], Source={x:Static 
    wpfApplication1:GlobalStats.srGlobalStatics}}" 
    HorizontalAlignment="Left" Margin="10,36,0,0" VerticalAlignment="Top"/>
KadekM
  • 1,003
  • 9
  • 13
  • @MonsterMMORPG Your class will have to be able to provide property change notifications, which is not possible when using a static class. If you were able to implement `INotifyPropertyChanged` then you'd be able to call `OnPropertyChanged("srGlobalStatics")`, which would fix your problem. – Forest Kunecke Aug 13 '14 at 22:19
  • Ok one more question. If i use ObservableCollection how can i bind it to the label. Assume that i added 1 element to the ObservableCollection and i always update that element. So i want to bind it to index 0 – Furkan Gözükara Aug 13 '14 at 22:24
  • To your first question http://stackoverflow.com/a/1925798/872413 ... also, I would like to suggest you to try and read a bit more about WPF and bindings, because I don't think you are on 'right track'. – KadekM Aug 13 '14 at 22:25
  • Edited answer to answer your second question – KadekM Aug 13 '14 at 22:40
0

The simplest (not necessarily the best) way to do this would be to declare your GlobalStats class as non-static, set it as your DataContext, and finally bind to your string from the XAML source:

public class GlobalStats
{
    private string _myLabel;

    public string MyLabel {
        get { return _myLabel; }
        set { _myLabel = value; }
    }
}

Then, in your code-behind, have something like this:

private GlobalStats_myDataModel = new GlobalStats();

// ...

public MainWindow() {
    // ...
    DataContext = _myDataModel;

Finally, your XAML:

<Label Name="lblGlobalStats" Content="{Binding Path=MyLabel" HorizontalAlignment="Left"
   Margin="10,36,0,0" VerticalAlignment="Top"/>
Forest Kunecke
  • 2,160
  • 15
  • 32