1

I didnt use WPF for a long time so I'm quite sure this is an easy question for most of you but here is my xaml code :

<Grid>
    <ProgressBar Name="Progress" Width="200" Height="20" Minimum="0" Maximum="100" Margin="10"/>
</Grid>

and here is the C# code :

namespace WpfApplication1
{
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private int _MyInt;
        public int MyInt
        {
            get { return _MyInt; }
            set
            {
                _MyInt = value;
                RaisePropertyChanged("MyInt");
            }
        }

        public MainWindow()
        {
            InitializeComponent();

            MyInt = 99;

            Random random = new Random();
            System.Timers.Timer aTimer = new System.Timers.Timer();
            aTimer.Elapsed += (sender, e) =>
            {
                MyInt = random.Next(0, 100);
            };
            aTimer.Interval = 500;
            aTimer.Enabled = true;

            Binding b = new Binding();
            b.Source = MyInt;
            b.Mode = BindingMode.OneWay;
            Progress.SetBinding(ProgressBar.ValueProperty, b);
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

When the application starts I got a 99 value on my ProgressBar so the binding seems to work but then, it doesn't refresh at all...

4
  • Progress.Value = MyInt; should do it no ? I tried with an ItemSource of a ListBox and it worked Commented Dec 1, 2011 at 14:33
  • take a look at this: stackoverflow.com/questions/2368479/wpf-data-binding-with-code Commented Dec 1, 2011 at 14:34
  • I know I can do a Binding "manually" but can't I but it this way ? (like in my code snippet) Commented Dec 1, 2011 at 14:36
  • 2
    All your snippet is doing is assigning a value to the value of your control. You need to explicitly set the binding for it to work (either in xaml or in the code) Commented Dec 1, 2011 at 14:44

2 Answers 2

3

Progress.Value = MyInt is simply setting the value to whatever the current value in MyInt is. This is not the same as binding the value, which means the value will point to MyInt, not be a copy of MyInt

To create a binding in the code-behind, it would look something like this:

Binding b = new Binding();
b.Source = this;
b.Path = new PropertyPath("MyInt");
Progress.SetBinding(ProgressBar.ValueProperty, b);

An alternative is to just bind the value in your XAML and update it as needed:

<ProgressBar Name="Progress" Value="{Binding MyInt}" />

Then in the code behind: MyInt = newValue;

Sign up to request clarification or add additional context in comments.

16 Comments

Code this strange error : Member System.Windows.Controls.Primitives.RangeBase.ValueProperty' cannot be accessed with an instance reference; qualify it with a type name instead
@GuillaumeCogranne Sorry, the binding needs to reference the ProgressBar class, not your ProgressBar Instance. I updated my answer
I got this error with ur new version : {"'The invocation of the constructor on type 'WpfApplication1.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'."}
If I comment the line "Progress.SetBinding(ProgressBar.ValueProperty, myBinding);" I got no more error but the binding doesnt work ofc.. !
And I got the same error if I try to bind in the xaml file :( Dunno if this has to do with the Timer im using
|
0

First, I don't think your window should be implementing the INotifyPropertyChanged. You are putting the data in your Window. You should have a separate class that implements INotifyPropertyChanged, and then set it as a DataContext to your Window. After that you need to add a Binding either through code, or in XAml like this :

<ProgressBar Name="Progress" Width="200" Height="20" Minimum="0" Maximum="100" Margin="10" Value="67" Value="{Binding MyInt}"/>

9 Comments

I'm not sure about the necessity of INotifyPropertyChanged but the thing is that I'd like to code the binding in C# code (not xaml) !
INotifyPropertyChanged is typically used on the view model not on a view. Since you have code in your view code-behind, that doesn't really fit the architecture you're using.
Rachel posted the code needed to create Binding in C#. BUt you still need to INotifyPropertyChanged, since WPF can't know when you values change, unless it checks it everytime, very suboptimal, we need to let it know that something changed.
@mydogisbox even if it is in the Window, WPF has no way to know that the values changed, unless he calls Progress.Value = MyInt; everytime MyInt changes
and it that case Binding is useless
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.