WP7依赖属性绑定问题

时间:2010-10-18 06:18:28

标签: windows-phone-7 dependency-properties

我有一个包含列表框的用户控件。我想绑定到listboxes选择的item属性,所以我创建了一个依赖属性。

        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];
        }
    }

我绑定到这个属性:

   <UserControls:HorizontalListBox
   DataContext="{Binding HousePrices}"
       SelectedItem="{Binding SelectedPriceFrom, Mode=TwoWay}" >
   </UserControls:HorizontalListBox>

我的视图模型属性:

        private HousePrice _selectedPriceFrom;
    public HousePrice SelectedPriceFrom
    {
        get
        {
            return _selectedPriceFrom;
        }
        set
        {
            _selectedPriceFrom = value;
            NotifyOfPropertyChange("SelectedPriceFrom");
        }
    }

我可以看到dp已设置但是绑定到我的vm属性似乎不起作用。

编辑:

我认为问题在于DataContext的UserControl被设置为HousePrices(我的VM中的一个属性),而SelectedItem被设置为我的VM中的另一个属性。我猜它正试图找到相对于HousePrices的SelectedItem。

另一个怪癖是我正在使用Caliburn Micro框架。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

现在已修复。

我为ItemsSource添加了另一个依赖项属性,并确保将所有DP属性更改事件连接到用户控件属性。

以下是使用用户控件的方法:

    <UserControls:HorizontalListBox
       ItemsSource="{Binding PriceFromList}"
       SelectedItem="{Binding SelectedPriceFrom, Mode=TwoWay}">
    </UserControls:HorizontalListBox>

以下是我如何连接DP:

    /// <summary>
    /// Item Source
    /// </summary>
    public ObservableCollection<ObjWithDesc> ItemsSource
    {
        get 
        {
            return (ObservableCollection<ObjWithDesc>)GetValue(ItemsSourceProperty); 
        }
        set
        {
            SetValue(ItemsSourceProperty, value);
        }
    }

    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register(
            "ItemsSource",
            typeof(ObservableCollection<ObjWithDesc>),
            typeof(HorizontalListBox),
            new PropertyMetadata(OnItemsSourcePropertyChanged)
        );

    static void OnItemsSourcePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        ((HorizontalListBox) obj).OnItemsSourcePropertyChanged(e);
    }

    private void OnItemsSourcePropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        ObservableCollection<ObjWithDesc> objWithDescList = (ObservableCollection<ObjWithDesc>)e.NewValue;

        MainListBox.ItemsSource = objWithDescList;
    }

    /// <summary>
    /// Selected Item
    /// </summary>
    public ObjWithDesc SelectedItem
    {
        get
        {
            return (ObjWithDesc)GetValue(SelectedItemProperty);
        }
        set
        {
            SetValue(SelectedItemProperty, value);
        }
    }

    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register(
            "SelectedItem",
            typeof(ObjWithDesc),
            typeof(HorizontalListBox),
            new PropertyMetadata(OnSelectedItemPropertyChanged)
        );

    static void OnSelectedItemPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        ((HorizontalListBox)obj).OnSelectedItemPropertyChanged(e);
    }

    private void OnSelectedItemPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        ObjWithDesc selectedItem = (ObjWithDesc)e.NewValue;

        MainListBox.SelectedItem = selectedItem;
    }

    private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count > 0)
        {
            SelectedItem = (ObjWithDesc)e.AddedItems[0];
        }
    }