Databindings Don't Seem to Refresh

Databindings don't seem to refresh

Implement INotifyPropertyChanged on your class. If you have many classes that need this interface, I often find it helpful to use a base class like the following.

public abstract class ObservableObject : INotifyPropertyChanged
{

protected ObservableObject( )
{
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged( PropertyChangedEventArgs e )
{
var handler = PropertyChanged;
if ( handler != null ) {
handler( this, e );
}
}

protected void OnPropertyChanged( string propertyName )
{
OnPropertyChanged( new PropertyChangedEventArgs( propertyName ) );
}

}

Then you just have to make sure you raise the PropertyChanged event whenever a property value changes. For example:

public class Person : ObservableObject {

private string name;

public string Name {
get {
return name;
}
set {
if ( value != name ) {
name = value;
OnPropertyChanged("Name");
}
}
}

}

Data binding not updating

It seems to be a bug with the base ComboBox as well. It is not possible to get this binding source craziness to work correctly.

Xaml bindings don't update (and everything seems like it should work)

Thanks to a comment on the question:

Also be aware that it is possible to bind directly to static properties of a static class, even with change notification, thus eliminating the need for a singleton. See e.g. here: stackoverflow.com/a/41823852/1136211 

(and answer) I've had success with both a static and a non-static binding (FINALLY...).

For binding UI to a static class

The static class:

public static class StaticClass
{
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;

#region Properties

public static string _exampleProperty = "Default value";
public static string ExampleProperty
{
get
{
return _exampleProperty;
}
set
{
if (_exampleProperty != value)
{
_exampleProperty = value;
OnStaticPropertyChanged();
}
}
}

#endregion

private static void OnStaticPropertyChanged([CallerMemberName]string propertyName = null)
{
StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
}
}

How to bind to UI:

<TextBlock Text="{Binding Path=(local:StaticClass.ExampleProperty)}"/>

How to set the property:

StaticClass.ExampleProperty = "New value that automatically updates the UI :)";

For binding UI to a non-static class

Use the code from the other answer.

How to do the processing and keep GUI refreshed using databinding?

In my WPF applications I don't send the property change directly from the model to the GUI. It always goes via a proxy (ViewModel).

The property change events are put in a queue which is read from the GUI thread on a timer.

Don't understand how that can be so much more work. You just need another listener for your model's propertychange event.

Create a ViewModel class with a "Model" property which is your current datacontext. Change the databindings to "Model.Property" and add some code to hook up the events.

It looks something like this:

public MyModel Model { get; private set; }

public MyViewModel() {
Model = new MyModel();
Model.PropertyChanged += (s,e) => SomethingChangedInModel(e.PropertyName);
}

private HashSet<string> _propertyChanges = new HashSet<string>();

public void SomethingChangedInModel(string propertyName) {
lock (_propertyChanges) {
if (_propertyChanges.Count == 0)
_timer.Start();
_propertyChanges.Add(propertyName ?? "");
}
}

// this is connected to the DispatherTimer
private void TimerCallback(object sender, EventArgs e) {
List<string> changes = null;
lock (_propertyChanges) {
_Timer.Stop(); // doing this in callback is safe and disables timer
if (!_propertyChanges.Contain(""))
changes = new List<string>(_propertyChanges);
_propertyChanges.Clear();
}
if (changes == null)
OnPropertyChange(null);
else
foreach (string property in changes)
OnPropertyChanged(property);
}

Changing bound object doesn't update TextBox when focused

Your DataBindings are stale since you replaced the existing DataSource.

Try clearing and adding them back in:

this.textBox1.DataBindings.Clear();
this.textBox1.DataBindings.Add(new Binding("Text", this.myDataBindingSource, "Value", true));

DataBinding, refresh data if DataContext changes

You're not changing the DataContext. You're changing the value in the property that you set the DataContext to.

You don't need a Tmp property at all. Just change the DataContext in your event handler, e.g.:

DataContext = ar[++index];

Is it possible to 'refresh' WPF data bindings

This is apparently somewhat of a known issue:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/8eb8280a-19c4-4502-8260-f74633a9e2f2/

In short, a RadioButton (through .Net 3.5sp1) somehow kills bindings of other RadioButtons when when it's checked while trying to uncheck any other buttons. The simple fix (read: hack) is to assign each radiobutton a different GroupName and then they don't try to mess with eachother

Bound control doesn't update its value when object is null

Setting myobject to null merely drops the reference to that object for that variable name. The actual instantiated object is still bound in the DataBindings, so in order to change the value of that object, you would then need to access it from there via textbox.DataBindings["SerialNumber"].

If you set the SerialNumber to null, that's a different matter, of course. It may then display "-No Data-" for you as a default value for null.

If you wish to remove the binding to that object, then you'll need to call the Remove method on your DataBindings collection e.g.

private void RemoveSerialNumberBinding()
{
Binding serialNumberBinding = textBox1.DataBindings["SerialNumber"];
textBox1.DataBindings.Remove(serialNumberBinding);
}

https://msdn.microsoft.com/en-us/library/system.windows.forms.controlbindingscollection.remove%28v=vs.110%29.aspx



Related Topics



Leave a reply



Submit