Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to refresh all bindings in WPF?

Tags:

If my code looks somewhat like the code beneath, would it be possible to refresh all bindings directly or would I have to hard-code all the bindings to refresh?

Service-side:

[ServiceContract] public interface IMyServiceContract {     [OperationContract]     MyDataContract GetData(); } [ServiceBehavior] public class MyService {     [OperationBehavior]     public MyDataContract GetData() {         MyDataContract data = new MyDataContract();         data.val1 = "123";         data.val2 = "456";         return data;     } } [DataContract] public class MyDataContract {     [DataMember]     public string val1;     public string val2; } 

Client-side xaml (namespace boilerplate code omitted):

<Window x:Class="MyWindow" DataContext="{Binding RelativeSource={RelativeSource Self}}" Title="{Binding Path=val1, Mode=OneWay}">     <DockPanel>         <TextBlock Text="{Binding Path=val1, Mode=OneWay}"/>         <TextBlock Text="{Binding Path=val2, Mode=OneWay}"/>     </DockPanel> </Window> 

Client-side code-behing:

public partial class MyWindow {     MyServiceClient client = new MyServiceClient();     MyDataContract data;     public string val1 {get{return data.val1;}}     public string val2 {get{return data.val2;}}     DispatcherTimer updateTimer = new DispatcherTimer();      public MyWindow() {         timer.Interval = new TimeSpan(0, 0, 10);         timer.Tick += new EventHandler(Tick);         Tick(this, null);         timer.Start();         InitializeComponent();     }      void Tick(object sender, EventArgs e) {         data = client.GetData();         // Refresh bindings     } } 

Please disregard any coding standards violations in the example code since it is simply intended as an example for the intended use.

like image 805
Teo Klestrup Röijezon Avatar asked Mar 01 '11 16:03

Teo Klestrup Röijezon


People also ask

What is the default binding mode in WPF?

Default Data-binding It just defines which is the default binding mode for the control's property. In WPF different controls has different default data-binding modes. For example, TextBlock's Text property has one-way as default binding mode but a TextBox's Text property has a two-way binding mode.

What is mode TwoWay WPF?

TwoWay : This has the same behavior as OneWay and OneWayToSource combined. The bound property will update the user interface, and changes in the user interface will update the bound property (You would use this with a TextBox or a Checkbox , for example.)

How does data binding work in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

What is binding path in WPF?

Binding path syntax. Use the Path property to specify the source value you want to bind to: In the simplest case, the Path property value is the name of the property of the source object to use for the binding, such as Path=PropertyName . Subproperties of a property can be specified by a similar syntax as in C#.


1 Answers

Found the answer, seems like that calling PropertyChanged with the PropertyChangedEventArgs property name set to "" refreshes all bindings.
The DataContext changing worked too, although this felt a bit "cleaner".

like image 111
Teo Klestrup Röijezon Avatar answered Oct 05 '22 13:10

Teo Klestrup Röijezon