0

I'm trying to Bind the Text property of the TextBlock to StaticClass.Percent. Since I couldn't do that (Or could I?) I have defined the LoadingPercent in my ViewModel so that I can bind to it. It is still not working. How can I solve this? Or can I bind to the StaticClass directly and ignore the ViewModel approach?

<Window x:Class="TestBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:testBinding="clr-namespace:TestBinding"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <testBinding:ViewModel/>
    </Window.DataContext>
    <StackPanel>
        <TextBlock Width="100" 
                   HorizontalAlignment="Center"
                   Text="{Binding LoadingPercent}"/>
        <Button Content="Change" 
                Width="200" 
                Height="30" 
                Margin="0 20 0 0" 
                HorizontalAlignment="Center"
                Click="ChangeText"/>
    </StackPanel>
</Window>

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void ChangeText(object sender, RoutedEventArgs e)
    {
        StaticClass.Percentage = 10;
    }
}

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private double loadingPercent;
    public double LoadingPercent
    {
        get { return StaticClass.Percentage; }
        set
        {
            loadingPercent = value;
            RaisePropertyChanged("LoadingPercent");
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public static class StaticClass
{
    public static int Percentage { get; set; }
}
Vahid
  • 5,144
  • 13
  • 70
  • 146

1 Answers1

2

Here is a mistake:

private double loadingPercent;
public double LoadingPercent
{
    get { return StaticClass.Percentage; }
    set
    {
        loadingPercent = value;
        RaisePropertyChanged("LoadingPercent");
    }
}

You return in get the StaticClass.Percentage but you assign loadingPercent in set.

I am not sure why you need the static class after all, but if you want to ditch the viewmodel and bind directly to the static property, see here

Community
  • 1
  • 1
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
  • I'm doing some calculations in the static class which I need to show in the TextBlock in real-time. I've tried the accepted answer in the link you just gave, but I couldn't get it working. – Vahid Aug 19 '14 at 23:07
  • If you modify the static class property, then there is no way for the view to get the modification. You have to use `INotifyPropertyChanged` – thumbmunkeys Aug 19 '14 at 23:33
  • But I can't use `INotifyPropertyChanged` in an static class. – Vahid Aug 19 '14 at 23:43