I have a user control containing a listbox. I want to bind to the listboxes selected item property so I created a dependency property.
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 > 0)
{
SelectedItem = (HousePrice)e.AddedItems[0];
}
}
I bind to the property like this:
<UserControls:HorizontalListBox
DataContext="{Binding HousePrices}"
SelectedItem="{Binding SelectedPriceFrom, Mode=TwoWay}" >
</UserControls:HorizontalListBox>
My view model property:
private HousePrice _selectedPriceFrom;
public HousePrice SelectedPriceFrom
{
get
{
return _selectedPriceFrom;
}
set
{
_selectedPriceFrom = value;
NotifyOfPropertyChange("SelectedPriceFrom");
}
}
I can see the dp being set but the binding to my vm property does not seem to work.
Edit:
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.
Another quirk is that I'm using the Caliburn Micro framework.
Any ideas?