Search code examples
wpfdata-binding

How to prevent DataBinding to occur temporarily?


I have a program running under WPF and MVVM. I have some daily worked hours information that I can show in a daily, weekly or monthly view.

I did that using three data grids and showing only the one users want by binding the Visible property of them. The problem es that those three grids are bound to an observable collection so the view is asking for information although this is not being shown. Is it possible to deactivate the data binding feature of some element by any property?


Solution

  • You can simply set the ItemsSource for the data grids that you don't display to null.

    You can also automate this by using a MultiBinding for the ItemsSource along with a custom IMultiValueConverter. The converter would bind to two values (the source collection and the grid's Visibility) and return the collection itself if the grid is visible or null if it's not. The binding itself can be one-way.