Note that there are some explanatory texts on larger screens.

plurals
  1. POWP7 Dependency Property Binding Problems
    text
    copied!<p>I have a user control containing a listbox. I want to bind to the listboxes selected item property so I created a dependency property.</p> <pre><code> public HousePrice SelectedItem { get { return (HousePrice)GetValue(SelectedItemProperty); } set { SetValue(SelectedItemProperty, value); } } public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register( "SelectedItem", typeof(HousePrice), typeof(HorizontalListBox), null ); private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count &gt; 0) { SelectedItem = (HousePrice)e.AddedItems[0]; } } </code></pre> <p>I bind to the property like this:</p> <pre><code> &lt;UserControls:HorizontalListBox DataContext="{Binding HousePrices}" SelectedItem="{Binding SelectedPriceFrom, Mode=TwoWay}" &gt; &lt;/UserControls:HorizontalListBox&gt; </code></pre> <p>My view model property:</p> <pre><code> private HousePrice _selectedPriceFrom; public HousePrice SelectedPriceFrom { get { return _selectedPriceFrom; } set { _selectedPriceFrom = value; NotifyOfPropertyChange("SelectedPriceFrom"); } } </code></pre> <p>I can see the dp being set but the binding to my vm property does not seem to work.</p> <p>Edit:</p> <p>I think the problem is to do with the DataContext for the UserControl being set to HousePrices (one property in my VM) and SelectedItem being set to another property in my VM. I'm guessing that it is trying to find SelectedItem relative to HousePrices.</p> <p>Another quirk is that I'm using the Caliburn Micro framework.</p> <p>Any ideas?</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