代码之家  ›  专栏  ›  技术社区  ›  Steve Chadbourne

WP7依赖属性绑定问题

  •  0
  • Steve Chadbourne  · 技术社区  · 14 年前

    我有一个包含列表框的用户控件。我要绑定到ListBox Selected项属性,因此创建了依赖项属性。

            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设置为HousePrices(我的虚拟机中的一个属性),并将SelectedItem设置为我的虚拟机中的另一个属性。我猜它是在试图找到与房价相关的精选项目。

    有什么想法吗?

    1 回复  |  直到 14 年前
        1
  •  0
  •   Steve Chadbourne    14 年前

    现在固定。

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

    下面是如何使用用户控件:

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

    下面是我如何连接DPS的方法:

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