0

我想根据 aVisbilityExpander选定值设置 a ComboBox。这ComboBox已经从以下映射到一个对象Model

<ComboBox Name="SelectedCar" ItemsSource="{Binding Path=CarCategories}" SelectedValue="{Binding Path=Car.CarCategory, Mode=TwoWay}"/>

我在 VM 中设置了一个以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更改事件没有被订阅(而且似乎我不能真正来自模型)所以ExtraCarDetailsVisibility当我更改汽车类别时,该属性永远不会被召回......

你会怎么做?谢谢!

4

1 回答 1

0

那里有几个问题:

  1. 尽管在 Car 对象中实现,但我缺少从 ICar 接口中的 INotifyPropertyChanged 继承 - 所以无法到达那里的 PropertyChanged 事件。
  2. 每次更改 ComboBox 类别时,我都必须更新代码才能触发事件。为此,我必须:

将一些事件挂钩到 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");
        }
    }
于 2013-08-19T09:58:02.230 回答