silverlight - 绑定(bind)属性 setter 没有被调用

标签 silverlight

我对以下场景有疑问(为简洁起见,代码被删减)。基本上,当设置依赖项属性时,不会调用我的用户控件属性的 Setter,我需要解决这个问题。

我的 View.xaml 中有以下代码

<Filter:Filter x:Name="ProductFilter" PrimaryItemSource="{Binding CarrierProducts}"  />

在 View.xaml.cs
    public ProductPricing()
    {
        InitializeComponent();

        ViewModel.Filter.ProductPricing vm = new ViewModel.Filter.ProductPricing();
        this.DataContext = vm;
    }

在我的 ViewModel 中,我公开了一个属性
    public ObservableCollection<Model.FilterItem> _carrierProducts;
    public ObservableCollection<Model.FilterItem> CarrierProducts
    {
        get
        {
            return _carrierProducts;
        }
        set
        {
            if (_carrierProducts != value)
            {
                _carrierProducts = value;
                RaisePropertyChanged("CarrierProducts");
            }
        }
    }

最后,Filter User 控件定义如下。
   public static readonly DependencyProperty PrimaryItemSourceProperty =
        DependencyProperty.Register("PrimaryItemSource", typeof(ObservableCollection<Model.FilterItem>), typeof(Filter), new PropertyMetadata(null));

    public ObservableCollection<Model.FilterItem> PrimaryItemSource
    {
        get
        {
            return (ObservableCollection<Model.FilterItem>)GetValue(PrimaryItemSourceProperty);
        }

        set
        {
            SetValue(PrimaryItemSourceProperty, value);

            ComboBox combo = _filters.ElementAt(0);
            FilterSourceChange(combo, value);
        }
    }

由于某种原因,设置了 PrimaryItemSource 属性,但未调用 Setter。我是否必须向 PropertyMetadata 对象添加一个 PropertyChange 事件来处理这个问题,因为这看起来像是很多简单的代码。

最佳答案

这就是需要在集合上运行额外代码的依赖属性应该如何编写:-

 public ObservableCollection<Model.FilterItem> PrimaryItemSource
 {
     get { return (ObservableCollection<Model.FilterItem>)GetValue(PrimaryItemSourceProperty); }
     set { SetValue(PrimaryItemSourceProperty , value); }
 }

 public static readonly DependencyProperty PrimaryItemSourceProperty =  
    DependencyProperty.Register(
    "PrimaryItemSource",
    typeof(ObservableCollection<Model.FilterItem>),
    typeof(Filter), new PropertyMetadata(null, OnPrimaryItemSourceChanged));  


 private static void OnPrimaryItemSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
 {
      Filter filter = (Filter)d;
      var oldValue = (ObservableCollection<Model.FilterItem>)e.OldValue;
      var newValue = (ObservableCollection<Model.FilterItem>)e.NewValue;
      filter.OnPrimaryItemSourceChanged(oldValue, newValue);
 }

 protected virtual void OnPrimaryItemSourceChanged(
     ObservableCollection<Model.FilterItem> oldValue,
     ObservableCollection<Model.FilterItem> newValue)
 {
     ComboBox combo = _filters.ElementAt(0);        
     FilterSourceChange(combo, newValue); 
 }

您可以在类中放置一个静态 DependencyPropertyChanged 处理程序,该处理程序会将依赖项对象转换为正确的类型,然后调用实例方法来提醒该实例的更改。

只要通过 SetValue 更改底层依赖属性,就会调用此更改处理程序。调用属性(property)Set方法或通过绑定(bind)或任何其他方式。

关于silverlight - 绑定(bind)属性 setter 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2237981/

相关文章:

silverlight - 相当于 silverlight 项目中的自动映射器

c# - System.Device.Location.GeoCooperativeWatcher.Position.Location.Speed 始终为 NaN

c# - 如何将附加本地对象变量传递给我的事件处理程序?

silverlight - WIndows Phone 中的 Grid 和 ItemsControl.ItemContainerStyle

c# - 在 Silverlight 中,如何知道代码是否在 UI 线程中运行?

silverlight - Silverlight 4 中的自定义控件是否需要位于单独的 DLL 中?

c# - 如何从 silverlight 文件夹中打开文件

c# 使用 + XmlWriter.Create = "Cannot access a closed Stream."

c# - 在图像按钮中加载图像

Silverlight MVVM 链接模型和 View 模型