派生的财产从未被召唤过

时间:2013-08-15 11:31:28

标签: c# xaml properties triggers

我想根据Visbility的选定值设置Expander的{​​{1}}。 ComboBox已经映射到ComboBox

中的对象
Model

我在VM中设置了一个以<ComboBox Name="SelectedCar" ItemsSource="{Binding Path=CarCategories}" SelectedValue="{Binding Path=Car.CarCategory, Mode=TwoWay}"/> 方式导出的属性:

Visbility

这就是我使用它的方式:

private Visibility _extraCarDetailsVisibility;
public Visibility ExtraCarDetailsVisibility
{
    get
    {
        if (ManagedPortfolioSelected != null)
        {
            var category = Car.CarCategory.ToLower();
            if (category == "porsche")
            {
                _extraCarDetailsVisibility= Visibility.Visible;
            }
        }
        _extraCarDetailsVisibility=  Visibility.Collapsed;
        return _extraCarDetailsVisibility;
    }
    set 
    { 
        _extraCarDetailsVisibility= value;
        NotifyPropertyChanged("ExtraCarDetailsVisibility");
    }
}

然而,这不起作用,因为我认为<Expander Visibility="{Binding Path=ExtraCarDetailsVisibility}"> 更改事件未订阅(而且似乎我不能真正来自模型)所以CarCategories属性当我改变汽车类别时永远不会被召回......

你会怎么做?谢谢!

1 个答案:

答案 0 :(得分:0)

那里有几个问题:

  1. 虽然我在ICar界面中继承了INotifyPropertyChanged,但我很遗憾在Car对象中实现了 - 因此无法在那里到达PropertyChanged事件。
  2. 我必须更新代码才能在每次更改ComboBox Catgory时触发事件。为此,我不得不:
  3. 将一些事件挂钩到Car对象的PropertyChanged事件:

        private ICar _carSelected;
        public IPortfolio CarSelected
        {
            get { return _carSelected; }
            set
            {
                if (_carSelected!= value)
                {
                    if (_carSelected!= null)
                    {
                        _carSelected.PropertyChanged -= OnCarSelectedPropertyChanged;
                    }
                    _carSelected= value;
                    // Also subscribing to any change that happens to the Carselected
                    _carSelected.PropertyChanged += OnCarSelectedPropertyChanged;
                }
                // Notifying when the selected portfolio is changed
                NotifyPropertyChanged("CarSelected");
            }
        }
    

    CarSelected属性更改后触发事件:

    void OnCarSelectedPropertyChanged(object sender, PropertyChangedEventArgs e)    
    {
        if (e.PropertyName == "CarCategory")
        {
            NotifyPropertyChanged("ExtraCarDetailsVisibility");
        }
    }
    

    ...那将更新我的可见性属性我已经连接到视图:

        private Visibility _extraCarDetailsVisibility;
        public Visibility ExtraCarDetailsVisibility
        {
            get
            {
                _extraCarDetailsVisibility= Visibility.Collapsed;
                if (PendingCarSelected != null)
                {
                    var category = CarSelected.CounterpartyCategory.ToLower();
                    if (category == "porsche" || category == "porsches")
                    {
                        _extraCarDetailsVisibility= Visibility.Visible;
                    }
                }
                return _extraCarDetailsVisibility;
            }
            set
            {
                _extraCarDetailsVisibility= value;
                NotifyPropertyChanged("ExtraCarDetailsVisibility");
            }
        }