Note that there are some explanatory texts on larger screens.

plurals
  1. POMvvM with custom View-Elements. Data Binding problems
    text
    copied!<p>As a newbie in Data-Binding, I don't know what I am doing wrong.</p> <ul> <li><p>I have some GUI elements defined in XAML, and I have data-binded them with appropriate ViewModels. So far so good.</p></li> <li><p>I also have some custom elements (geometrical shapes) that I place inside a Canvas (which Canvas I place inside the mainwindow through a user-control). I derived these entities from FrameworkElement, to have support for data-binding.</p></li> </ul> <p>So what I have done is to register some DependencyProperties and set the bindings to one of the existing ViewModels, as it seemed to me logical.</p> <p>Now the DependencyProperties of these custom classes, display some strange behaviour. </p> <p>i) When I keep the focus only on the Views (controls) that use the same ViewModel with the custom elements, the properties update normally. If I click everywhere else, the bindings break.</p> <p>ii) Sometimes, the Callback wasn't called although the property was changing.</p> <p>iii) When the StartupURI in App.xaml was the MainWindow, if I declared the ProfileV as a field (no matter where it was instantiated), the databinding mechanism worked in the way of (i). If it was declared inside the constructor, the mechanism didn't worked.</p> <p>What I am doing wrong, and what crucial thing i misunderstand about databinding ??</p> <pre class="lang-cs prettyprint-override"><code>class ProfileV : FrameworkElement, IGraphicalElement { public int SelectedTab { get { return (int)GetValue(SelectedTabProperty); } set { SetValue(SelectedTabProperty, value); } } public static readonly DependencyProperty SelectedTabProperty = DependencyProperty.Register("SelectedTab", typeof(int), typeof(ProfileV), new PropertyMetadata(new PropertyChangedCallback(CallBack))); public ProfileV(GeneralExecutionVM VM,CanvasV canvasV) { DataContext = VM; BindingOperations.SetBinding(this, SelectedTabProperty, new Binding("SelectedTab")); } public static void CallBack(DependencyObject d, DependencyPropertyChangedEventArgs e) { } } </code></pre> <p>Which binds with this</p> <pre class="lang-cs prettyprint-override"><code>public class GeneralExecutionVM : ObservableObject { private int _SelectedTab; public int SelectedTab { get { return _SelectedTab; } set { if (_SelectedTab == value) return; _SelectedTab = value; base.RaisePropertyChanged("SelectedTab"); } } } </code></pre> <p>(Observable Object, is the base class from the MVVM Foundation, of Josh Smith.) </p> <p><strong>ANSWERED</strong><br> OK i found it. The misconception here is about the <strong>DataContext</strong>.<br> Be careful when and where you set it, against setting an <strong>explicit source object</strong>. I misused it here, and caused a small chaotic situation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload